Python operator overloading: Why x + y and y + x give different errors

Python operator overloading: Why x + y and y + x give different errors

Operators in Python are turned into “magic” method calls:

x + y # becomes x.__add__(y)

So:

x = 10
y = 'hello'
x + y  # unsupported operand type(s) for +: 'int' and 'str'
y + x  # can only concatenate str (not "int") to str

Different errors from different methods!