-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
Closed
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir
Description
Documentation
6.3.1. Attribute references
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:
class C:
def __init__(self):
self.a = 1
def __getattr__(self, attr): # called when attribute not found
print('attribute does not exist')
def __getattribute__(self, attr): # called first
print('attribute', attr, 'accessed')
return object.__getattribute__(self, attr) # object is the top-level type for classes
o = C()
print(o.a) # found, calls __getattribute__
print(o.b) # not found, calls both methods.Hence, either mention both methods or only __getattribute__.
Linked PRs
Metadata
Metadata
Assignees
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir