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?
def __getitem__(self, y): """ x.__getitem__(y) <==> x[y] """
– Kirmess__getattr__
. In other words, why isobj[x]
not equivalent toobj.__getattr__("__getitem__")(x)
. To the OP: If that were the case, it would imply that__getattr__
would have to be sent to itself ;) – Horn