class A(object):
def __radd__(self, other):
print ("__radd__ called of A")
return "result of A"
class B(object):
def __radd__(self, other):
print ("__radd__ called of B")
return "result of B"
print (B()+A())
print (A()+B())
>>__radd__ called of A
>>result of A
>>__radd__ called of B
>>result of B
Documentation
object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
object.__rdiv__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
object.__rdivmod__(self, other)
object.__rpow__(self, other)
object.__rlshift__(self, other)
object.__rrshift__(self, other)
object.__rand__(self, other)
object.__rxor__(self, other)
object.__ror__(self, other)
These methods are called to implement the binary arithmetic operations
(+, -, *, /, %, divmod(), pow(), **, <<, >>, &, ^, |) with reflected
(swapped) operands. These functions are only called if the left
operand does not support the corresponding operation and the operands
are of different types. [2]