What a curious assignment.

bonono at gmail.com bonono at gmail.com
Wed Nov 23 03:17:32 EST 2005


Steven D'Aprano wrote:
> 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.)
>
I believe he knows about inheritance, but not about the behaviour of
the assignment. In many other OO languages, I believe you cannot have
the same name for both instance variable and class variable. javascript
has similar behaviour.




More information about the Python-list mailing list