In Python, we use “or” for conditions. | is bitwise (not boolean) “or”: x | y # 15, or 0b1111 | runs __or__. On dicts, | combines.
The % in Python, on numbers, is modulo: But str uses __mod__ for interpolation: Same operator, same magic method — but totally different.
Just as + in Python invokes __add__, other operators invoke other magic methods: • – is __sub__• * is __mul__• / is __truediv__• // is __floordiv__• % is __mod__• ** is __pow__ Your methods can do whatever you want. But do try to stick to conventional expectations.
I’ve been using Claude Code several hours a day for months, and I’m having a blast. But an agent does what you tell it, not what you meant — which is why validating your results now matters more than the results themselves.
If you tell Python it’ll run x.__add__(5), which handles the int 5. But what about int.__add__ doesn’t know how to handle x. So it returns NotImplemented — and Python turns it around, calling: That’s the “reverse add” magic method!
If your Python class implements __add__, it should usually return a new instance of your class, not an int:
How can your Python object support +? Implement __add__:
Operators in Python are turned into “magic” method calls: x + y # becomes x.__add__(y) So: Different errors from different methods!
Because Python functions’ defaults are kept in __defaults__, avoid mutable defaults: add1.__defaults__ # ([1, 1, 1],)
When a Python generator yields, its stack frame remains — inspect it for the current line and local variables: