0

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.

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 get the result of running this line print(s) to be <__main__.Stack object at 0x00000000032CD748>t when I would expect and am looking for ['plate 1','plate 2','plate3']

4
  • Please fix your ident Commented Apr 17, 2013 at 22:51
  • what's wrong about deque? Commented Apr 17, 2013 at 22:54
  • 1
    @deathApril -- Since OP is really just wrapping a list, I assume that this is mostly a learning exercise... Commented Apr 17, 2013 at 22:56
  • Yes, if this wasn't a learning exercise, list would be fine on its own. Commented Apr 17, 2013 at 23:14

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep a learning exercise, new to programming and realized something somewhere was defaulting to the memory address, but couldnt figure out how to correctly overide it. Thanks
2

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.

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

1 Comment

Why would you use self.__dict__['x'] when you can just use self.x?
0

Yes override __str__ and/or __repr__ Remember __repr__ can can evaled and return the same object if possible

1 Comment

__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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.