Why is __getattribute__ not invoked on an implicit __getitem__-invocation?

前端 未结 1 1428
鱼传尺愫
鱼传尺愫 2020-12-16 02:45

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

相关标签:
1条回答
  • 2020-12-16 03:08

    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).

    0 讨论(0)
提交回复
热议问题