I have created my own LIFO container class Stack that supports the methods of push, len, pop, and a check on isEmpty. All methods appear to be working in my example calls, except for when I call a created instance of this class(in my example s) I receive a memory location for the created object when I want to see the actual contents of that object.

3 Answers
3

You need to also override __str__ or __repr__ if you want your class to have a different representation when printing. Something like:

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

should do the trick. __str__ is what is called by the str function (and implicitly called by print). The default __str__ simply returns the result of __repr__ which defaults to that funny string with the type and the memory address.

You need to override the default implementation of __repr__. Otherwise it will use the default implementation which returns an informal string representation of the class, in this case the type and memory address.

__repr__ can be evaled and return the same object -- That's not exactly true. If possible/practical, it should follow that guideline. But for most real-world objects, this isn't even practical.
–
mgilsonApr 17 '13 at 23:04