Python's object model
Francois Bedard
francois.bedard at usa.net
Sun Apr 11 21:49:46 EDT 1999
Thank you both, Charles and Florent. I'm most familiar with Eiffel's
object model and apparently generalized a bit hastily, but your answers
did clarify things...
Francois
Francois Bedard wrote :
>
> I'm new at Python. In the following program, "stack_1 pops stack_2's
> content" (that's actually what it prints), which is obviously not what
> I'm after - nor would expect. [...]
>
> class Stack:
> stack = []
> [...]
>
> class System:
> stack_1 = Stack()
> stack_2 = Stack()
> [...]
Charles G Waldman wrote :
>
> This "Stack" class [...]
> 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 = ()
> [...]
Florent Heyworth wrote :
>
> what you're seeing is the difference between a class and an
> instance variable. To see the behaviour you want you need to
> modify your stack class as follows:
>
> class Stack:
> def __init__(self):
> self.stack = []
> [...]
>
> Otherwise the stack is a class variable which can be referred as
> Stack.stack() (this would then act as a class global).
> [...]
More information about the Python-list
mailing list