Arithmetic Dunder Methods PREMIUM

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
8 min. read Python 3.8—3.12
Tags

How can you control the arithmetic operators on your Python objects, like +, *, and /?

It's all about dunder methods

You can overload arithmetic operators by implementing the right dunder methods.

When Python sees a + operator between two objects, it will call the __add__ method on the first object, passing in the second object:

...

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.