I've got the following code under python command line. I expect that when I visit an attribute, getattribute is automatically called. But I don't see the result
class D:
def __getattribute__(s,*a,**ka):
print "__getattribute__"
return object.__getattribute__(s,*a,**ka)
def __getattr__(s,name):
print "__getattr__"
return name
def __get__(s,instance,owner):
print "__get__"
return s
a='abc'
class C2(object):
d=D()
>>> d=D()
>>> c2=C2()
>>> print d.a
abc
>>> print d.zzz
__getattr__
No "getattribute" printed. Why?
You're running Python 2, and your D
class is old-style, not new-style. __getattribute__
is used for new-style classes only.
As a rule, I put the following at the top of all modules that might be run under Python 2, to ensure all classes defined are implicitly new-style (and cause no harm on Py3, where it's ignored):
__metaclass__ = type
Alternatively, you can explicitly inherit from object
(or any other new-style class that fits your needs) to get new-style classes.