Hello,
bellow is a simple Python2 example of a class which defines __getattr__ method
and a property, where AttributeError exception is raised:
from __future__ import print_function
class MyClass(object):
def __getattr__(self, name):
print('__getattr__ <<', name)
raise AttributeError(name)
return 'need know the question'
@property
def myproperty(self):
print(self.missing_attribute)
return 42
my_inst = MyClass()
print(my_inst.myproperty)
# produces following output
__getattr__ << missing_attribute
__getattr__ << myproperty
Traceback (most recent call last):
File "a.py", line 84, in <module>
main()
File "a.py", line 74, in main
print('==', my_inst.myproperty)
File "a.py", line 36, in __getattr__
raise AttributeError(name)
AttributeError: myproperty
By the documentation
https://docs.python.org/2/reference/datamodel.html#object.__getattr__ , if
class defines __getattr__ method, it gets called at AttributeError exception
and should return a computed value for name, or raise (new) AttributeError
exception.
Why is __getattr__ called 2nd time, with 'myproperty' argument ?!?
self.myproperty does exist and also at first call of __getattr__ new
AttributeException with 'missing_attribute' is raised. I can't see any reason
for this behavior.
--
https://mail.python.org/mailman/listinfo/python-list