Python: Why is __getattr__ catching AttributeErrors?

exception, python, python-2.7, python-3.x

Solution

I just changed the code to

class A(object):
    @property
    def a(self):
        print "trying property..."
        raise AttributeError('lala')
    def __getattr__(self, name):     
        print('attr: ', name)
        return 1      

print(A().a)

and, as we see, indeed the property is tried first. But as it claims not to be there (by raising `AttributeError`), `__getattr__()` is called as "last resort".

It is not documented clearly, but can maybe be counted under "Called when an attribute lookup has not found the attribute in the usual places".

Problem

I'm struggling with `__getattr__`. I have a complex recursive codebase, where it is important to let exceptions propagate. ``` class A(object): @property def a(self): raise AttributeError('lala') def __getattr__(self, name): print('attr: ', name) return 1 print(A().a) ``` Results in: ``` ('attr: ', 'a') 1 ``` Why this behaviour? Why is no exception thrown? This behaviour is not documented (`__getattr__` documentation). `getattr()` could just use `A.__dict__`. Any thoughts?

Original source