Python's object model

Charles G Waldman cgw at fnal.gov
Sun Apr 11 02:58:43 EDT 1999


This "Stack" class:


class Stack:
        stack = []

        def push(self, value):
                self.stack.append(value)

        ...

doesn't work as you expected because the way you've declared it, the
".stack" attribute is a class attribute (shared by all instances of
the class) rather than an instance attribute.  To create instance
attriubtes, create them in the initializer, like this:

class Stack:
        def __init__(self):
            self.stack = ()

	def push(self, value):
	...

Also note that in the not-quite-yet released Python 1.5.2 the builtin
list objects have a pop() method, which is kind of handy.




More information about the Python-list mailing list