I'm currently learning python operator overloading (__radd__
and __add__
to be exact) and I have the following code
class Commuter1:
def __init__(self, val):
self.val = val
def __add__(self, other):
print('add', self.val, other)
return self.val + other
def __radd__(self, other):
print('radd', self.val, other)
return other + self.val
x = Commuter1(88)
y = Commuter1(99)
print(x + y)
I have got the following result
When used separately, I understand how __radd__
and __add__
works. But for the line x + y
, I'm not sure why both __radd__
and __add__
methods are evoked.