def __init__(self, list_to_be_wrapped):
self._items = list_to_be_wrapped
def __getattr__(self, item):
return getattr(self._items, item)
MyList([1, 2, 3])[-1]
TypeError: ‘MyList’ object does not support indexing
MyList([1, 2, 3]).pop()
will work perfectly (
pop will be intercepted by the
__getattr__ and redirected to
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?
Best How To :
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:
__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.