Python’s __radd__: How reflected operators handle 5 + x

Python's __radd__: How reflected operators handle 5 + x

If you tell Python

    x + 5

it’ll run x.__add__(5), which handles the int 5.

But what about

    5 + x

int.__add__ doesn’t know how to handle x. So it returns NotImplemented — and Python turns it around, calling:

    x.__radd__(5)

That’s the “reverse add” magic method!