Classes Python and developing Stack -


i have created own lifo container class stack supports methods of push, len, pop, , check on isempty. methods appear working in example calls, except when call created instance of class(in example s) receive memory location created object when want see actual contents of object.

class stack:     x = []      def __init__(self, x=none):         if x == none:             self.x = []         else:             self.x = x      def isempty(self):         return len(self.x) == 0     def push(self,p):         self.x.append(p)     def pop(self):         return self.x.pop()     def __len__(self):         return(len(self.x))       s = stack()         s.push('plate 1')     s.push('plate 2')     s.push('plate 3')     print(s)     print(s.isempty())     print(len(s))     print(s.pop())     print(s.pop())     print(s.pop())     print(s.isempty()) 

i result of running line print(s) <__main__.stack object @ 0x00000000032cd748>t when expect , looking ['plate 1','plate 2','plate3']

you need override __str__ or __repr__ if want class have different representation when printing. like:

def __str__(self):     return str(self.x) 

should trick. __str__ called str function (and implicitly called print). default __str__ returns result of __repr__ defaults funny string type , memory address.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -