Class attributes, instances and metaclass __getattribute__

Pedro Werneck pedro.werneck at terra.com.br
Mon Aug 7 18:28:46 EDT 2006


Hi all


I noticed something strange here while explaining decorators to someone.
Not any real use code, but I think it's worth mentioning. 

When I access a class attribute, on a class with a custom metaclass with
a __getattribute__ method, the method is used when acessing some
attribute directly with the class object, but not when you do it from
the instance.

<code type='prompt'>
>>> class M(type):
...     def __getattribute__(cls, attr):
...             print cls, attr
...             return type.__getattribute__(cls, attr)
... 
>>> class C(object):
...     __metaclass__ = M
... 
>>> C.x = 'foo'
>>> C.x
<class '__main__.C'> x
'foo'
>>> o = C()
>>> o.x
'foo'
>>> 
</code>


Someone at freenode #python channel involved with python-dev sprint
suggested it might be a bug, worth mentioning... to me it seems like a
decision to avoid some problems with method and descriptors creation,
since someone using metaclasses and custom __getattribute__ at the same
time is asking for trouble, but... I googled for it and tried to find
something on the list but, nothing. From the source it seems like a
generic wrapper is used. What's the real case here ? 


Regards,

-- 
Pedro Werneck



More information about the Python-list mailing list