I am trying to customize attribute access in a class (python2.7). Say I define the following class:
class testAttrAccess(object):
def __init__(self):
self.data = {"attr1":"value1","attr2":"value2"}
def __getattr__(self,name):
try:
return self.data[name]
except KeyError:
pass
raise AttributeError
Then I get:
In [85]: a = testAttrAccess()
In [86]: a.attr2
Out[86]: 'value2'
In [87]: a.__getattr__('attr2')
Out[87]: 'value2'
In [88]: a.__getattribute__('attr2')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Users/laserson/temp/<ipython console> in <module>()
AttributeError: 'testAttrAccess' object has no attribute 'attr2'
However, according to the python documentation
If the class also defines
__getattr__(), the latter will not be called unless__getattribute__()either calls it explicitly or raises anAttributeError.
So if __getattribute__() is raising an AttributeError, then why isn't __getattr__() being called and returning the proper value?