I have a class, that is a container for members. all members are of the same type.
class A(int):
def __init__(self, n):
super().__init__()
self.n = n
def do(self):
print('adding 10')
return self.n + 10
class B:
def __init__(self, a1, a2):
self.a = [a1, a2]
def __getattr__(self, item):
return getattr(self.a[0], item)
a1 = A(10)
a2 = A(5)
b = B(a1, a2)
the __getattr__
overrides the do
method:
In[7]: b.do()
adding 10
Out[7]: 20
and even overrides __add__
when called explicitly
In[8]: b.__add__(1)
Out[8]: 11
but __add__
fails when called as +
In[9]: b+1
TypeError: unsupported operand type(s) for +: 'B' and 'int'
How can I override magic methods to work as expected?