简体   繁体   中英

Why magic methods are not intercepted by __getattr__ implementation in python?

Subj.

Example:

MyList(object):

    def __init__(self, list_to_be_wrapped):
          self._items = list_to_be_wrapped

    def __getattr__(self, item):
          return getattr(self._items, item)

Then

MyList([1, 2, 3])[-1]

will raise: TypeError: 'MyList' object does not support indexing

while:

MyList([1, 2, 3]).pop()

will work perfectly ( pop will be intercepted by the __getattr__ and redirected to _items )

It seems for me that interception of magic methods by __getattr__ would be very helpful considering the case of "implementing own container types via composition not inheritance". Why is such "interception" not supported in python?

For the full answer, you need to look at the documentation for special method lookup , specifically:

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.

Which is justified by:

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

Note that this only applies for new-style classes (classes which extend object ), but you really should always use new-style classes for any new code.

The simplest solution is that you need to manually proxy all of the special methods you wish to redirect to the proxy'd class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM