direct initialization of class attributes vs. declarations w/in __init__

Fredrik Lundh fredrik at pythonware.com
Sun Jun 11 15:10:02 EDT 2006


digitalorganics at gmail.com wrote:

> At first, I thought that self.jerk was resolving to the class attribute
> instead of creating a new variable (w/ a differing scope).

When you access an instance attribute, Python first looks in the 
instance object, and then in the class object.

When you assign to an instance attribute, it's *always* assigned to the 
instance object, whether there's a class attribute with the same name or 
not.

If there's already a class attribute with the same name, that attribute 
will be shadowed (but can still be accessed via the class object, of 
course).

> It doesn't work this way on my laptop...

Python's always worked that way, so I find that a bit hard to believe.

</F>




More information about the Python-list mailing list