What a curious assignment.

Steven D'Aprano steve at REMOVEMEcyber.com.au
Wed Nov 23 03:05:57 EST 2005


Neil.Jin at gmail.com wrote:

> Is there somthing wrong????

Kids today, don't they learn about inheritence? :-)

Python's object model is that instances inherit both 
methods and attributes from the class (and 
superclasses). Methods are just a special case of 
attributes: the method is a callable attribute.

When you reference an attribute, Python first checks 
the instance by looking up instance.__dict__, and if 
that fails, it looks up instance.__class__.__dict__.

(This is a simplification, e.g. it isn't exactly true 
for objects with slots.)

For attribute lookup (that is, the attribute reference 
is on the right hand side of an assignment), the lookup 
may fail and so the class attribute may be retrieved. 
This is by design.

For attribute assignment (that is, the attribute 
reference is on the left hand side of an assignment), 
the assignment will never fail.

(Again, ignoring slots and any other special cases I 
have't thought of.)


-- 
Steven.




More information about the Python-list mailing list