X+1
first, calls:
X.__add__(1)
That succeeds, so no further work is needed.
On the other hand, this:
1+X
calls
(1).__add__(X)
That fails because int
doesn't know how to interface with a class a
. "As a last resort" this is tried instead:
X.__radd__(1)
From the docs on __radd__
:
These functions are only called if the left operand does not support the corresponding operation and the operands are of different types.