Skip to content

Reference Manual 6.3.1: wrong method mentioned #112331

Description

@aichernig

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

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions