Why property works only for objects?

Alex Martelli aleaxit at yahoo.com
Sat Mar 11 20:58:07 EST 2006


Michal Kwiatkowski <ruby at no.spam> wrote:
   ...
> Can you also check my reasoning for getting attributes?
> 
> value = obj.attr
>   * if instance class has __getattribute__, call it
>   * else: lookup "attr" in all parent classes using class __mro__;
>     if it's a descriptor call its __get__ method, return its value
>     otherwise (when descriptor doesn't have __get__, it's unreadable
>     and AttributeError is raised)
>   * else: check instance __dict__ for "attr", return it when found
>   * else: lookup __getattr__ in instance class and call it when found
>   * else: raise AttributeError

No, the value found in the instance (your second 'else' here) takes
precedence if the descriptor found in the first 'else' is
non-overriding.

The difference:

>>> class OD(object):
...   def __get__(*a): return 23
...   
>>> class NOD(object):
...   def __get__(*a): return 23
... 
>>> class OD(NOD):
...   def __set__(*a): pass
... 
>>> class WOD(object): zap=OD()
... 
>>> class WNOD(object): zap=NOD()
... 
>>> o=WOD(); o.zap=42; print o.zap
23
>>> o=WNOD(); o.zap=42; print o.zap
42
>>> 


Alex



More information about the Python-list mailing list