What a curious assignment.

Naveed naveedm9 at gmail.com
Wed Nov 23 00:20:48 EST 2005


"A.i" is a class attribute.  "a.i" at first is the same as "A.i".  Once
you set a.i = 2, you are actually creating a new data attribute called
i for the instance a.  This happens on the fly.  So then when you
reference a.i, it uses the instance data attribute, instead of the
class attribute.

This might make it more clear.  Try:
a.f = 3
print a.f
Even though f is not declared in your class definition, the above code
still prints 3.  Because it created the data attribute f on the fly.




More information about the Python-list mailing list