Caffeinated Coder
Book an appointment
Lesson 19 · Objects

Special (dunder) methods

Why can you write a + b, len(x) or print(x) for your own classes? Because Python turns that ordinary syntax into calls of special methods, the ones named with double underscores. Follow a small Coins class line by line as Coins(3) calls __init__, a + b calls __add__ and print(c) calls __repr__, then see what breaks when a special method is missing.

14 min9 steps5 questionsIntermediate
Fig 19 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.