-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Description
Documentation
The example below, from the Descriptor Guide, does not seem to reflect the behaviour of the actual implementation.
cpython/Doc/howto/descriptor.rst
Lines 733 to 740 in ced2691
| def getattr_hook(obj, name): | |
| "Emulate slot_tp_getattr_hook() in Objects/typeobject.c" | |
| try: | |
| return obj.__getattribute__(name) | |
| except AttributeError: | |
| if not hasattr(type(obj), '__getattr__'): | |
| raise | |
| return type(obj).__getattr__(obj, name) # __getattr__ |
Unless I'm mistaken, line 736 return obj.__getattribute__(name) should actually read return type(obj).__getattribute__(obj, name).
It's easy to miss when thinking of class instances, but the issue becomes more evident when the example is applied to an attribute lookup on a class object instead. The example would call the __getattribute__ method of the class itself, while the actual implementation calls __getattribute__ on the metaclass (not to mention that in this case the call in the example would fail, due to a missing argument, as it is calling a plain function, rather than a bound method).
Metadata
Metadata
Assignees
Labels
Projects
Status