|
|
|
|
|
by bilboa
2909 days ago
|
|
This is because the actual translation of `a + b` is a little more complicated. It's something like this: if '__add__' in a.__dict__:
try:
return a.__add__(b)
except NotImplemented:
if '__radd__' in b.__dict__:
return b.__radd__(a)
elif '__radd__' in b.__dict__:
return b.__radd__(a)
raise TypeError('unsupported operand type(s) for +: '{}' and '{}'.format(type(a), type(b))
In particular, the runtime seems to directly look for '__add__' in the object's __dict__, rather than just trying to invoke `__add__`, so your `__getattribute__` method isn't enough to make it work. If you add an actual `__add__` method to A your example will work. |
|
I'm reminded of PHP, where (at least in version 5.*) we could write:
(Taken from an old comment https://news.ycombinator.com/item?id=8119419 )