Class Variable Access and Assignment
Eric Nieuwland
eric.nieuwland at xs4all.nl
Thu Nov 3 09:13:52 EST 2005
Stefan Arentz wrote:
> It is really simple. When you say b.a then the instance variable 'a'
> is looked up first. If it does not exist then a class variable lookup
> is done.
This mixing of class and instance variable might be the cause of
confusion...
I think of it as follows:
1 When the class statement ends a class object is created which is
filled by all the statements inside the class statement
This means all variables and functions (methods) are created according
to the description.
NOTE This happens just once.
2 When an instance of the class is created, what effectively happens is
that a shallow copy of the class object is made.
Simple values and object references are copied.
This explains:
- why methods and complex objects (e.g. lists) are shared among
instances of a class and the class itself
- simple values are not shared
--eric
More information about the Python-list
mailing list