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:
As someone who teaches Python programming for a living, I’ve spent the last few years wrestling with the educational implications of AI. I’m changing everything I do to adjust to our new AI reality, experimenting with new ideas, including my AI-based Socratic tutor (https://practice.lernerpython.com/). I keep what works, throw away what doesn’t, and then try…
July and August are often when people take a break or go on vacation. But for me, this summer has been super busy, full of writing and improving LernerPython based on feedback I’ve gotten from people around the world. And so, I’m here with some big changes I’m making at LernerPython World Headquarters: 1. AI…