On Thu, 03 Nov 2005 15:13:52 +0100, Eric Nieuwland wrote: > 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.
Yes. > 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. No. py> class Parrot: ... var = 0 ... py> p = Parrot() py> Parrot.var is p.var True py> Parrot.var = {"Hello world": [0, 1, 2]} py> Parrot.var is p.var True It all boils down to inheritance. When Python does a look up of an attribute, it looks for an instance attribute first (effectively trying instance.__dict__['name']). If that fails, it looks up the class second with instance.__class__.__dict__['name'], and if that fails it goes into a more complex search path looking up any superclasses (if any). > 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 No. it is all about the inheritance, and mutable/immutable objects. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list