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?