This section states that "This production can be customized by overriding the __getattr__() method."
This is wrong and should be replaced with "This production can be customized by overriding the __getattribute__(self, attr) method.".
__getattr__() is only called when the attribute is not found. __getattribute__(self, attr) is the method that is called when accessing an attribute. Here is an example code for both:
classC:
def__init__(self):
self.a=1def__getattr__(self, attr): # called when attribute not foundprint('attribute does not exist')
def__getattribute__(self, attr): # called firstprint('attribute', attr, 'accessed')
returnobject.__getattribute__(self, attr) # object is the top-level type for classeso=C()
print(o.a) # found, calls __getattribute__print(o.b) # not found, calls both methods.
Hence, either mention both methods or only __getattribute__.
The primary must evaluate to an object of a type that supports attribute
references, which most objects do. This object is then asked to produce the
attribute whose name is the identifier. The type and value produced is
determined by the object. Multiple evaluations of the same attribute
reference may yield different objects.
This production can be customized by overriding the :meth:`__getattribute__`
method or the :meth:`__getattr__` method. The :meth:`__getattribute__`
is called first and either returns a value or raises :exc:`AttributeError`
if the attribute is not available.
If an :exc:`AttributeError` raised and the object has a :meth:`__getattr__`
method, that method is called as a fallback.
Yes almost perfect. I think the last sentence "If an :exc:AttributeError raised ... " should be "If an :exc:AttributeError is raised ..." or "was raised".
Activity
aichernig commented on Nov 24, 2023
What do you mean? I explained what the problem is and how it should be resolved.
rhettinger commented on Nov 24, 2023
@aichernig Will this revised text suffice?
pythongh-112331: Fix reference manual description of attribute lookup…
aichernig commented on Nov 24, 2023
Yes almost perfect. I think the last sentence "If an :exc:AttributeError raised ... " should be "If an :exc:AttributeError is raised ..." or "was raised".
gh-112331: Fix reference manual description of attribute lookup mecha…
pythongh-112331: Fix reference manual description of attribute lookup…
pythongh-112331: Fix reference manual description of attribute lookup…
[3.11] gh-112331: Fix reference manual description of attribute looku…
[3.12] gh-112331: Fix reference manual description of attribute looku…
pythongh-112331: Fix reference manual description of attribute lookup…
pythongh-112331: Fix reference manual description of attribute lookup…