puzzlement about classmethod
Duncan Booth
duncan.booth at invalid.invalid
Sat Jun 24 16:38:54 EDT 2006
Faheem Mitha wrote:
> Nearly everything behaves the way I would expect, except that setting
> x to 12 in A using class_setx at the beginning also sets the value for
> x in B. However, the converse (setting x in B using class_setx), does
> not change the value in A, which I would consider to be the expected
> behavior.
>
> However, after that the two x variables appear to behave
> independently. Can anyone explain to me why this is so?
When you access a class variable B.x before it has been set in B then you
see the value from the base class (A.x). Once you have set a separate value
in the subclass that takes precedence.
Similarly accessing an instance variable b.x first looks in the instance
(b), then in the class (B), then in the base class or classes (here A). The
lookup stops as soon as it finds a value in any of these locations.
More information about the Python-list
mailing list