Skip to content Skip to sidebar Skip to footer

Why Is __getattribute__ Not Invoked On An Implicit __getitem__-invocation?

While trying to wrap arbitrary objects, I came across a problem with dictionaries and lists. Investigating, I managed to come up with a simple piece of code whose behaviour I simpl

Solution 1:

Magic __methods__() are treated specially: They are internally assigned to "slots" in the type data structure to speed up their look-up, and they are only looked up in these slots. If the slot is empty, you get the error message you got.

See Special method lookup for new-style classes in the documentation for further details. Excerpt:

In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the __getattribute__() method even of the object’s metaclass.

[…]

Bypassing the __getattribute__() machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method must be set on the class object itself in order to be consistently invoked by the interpreter).

Post a Comment for "Why Is __getattribute__ Not Invoked On An Implicit __getitem__-invocation?"