Special (Dunder) Methods
LockedLesson 18 of 60 · OOP · Challenging
Mastery0% of 85%
Up next: Variables and Types (~7 min) →Finish these first:Classes and Objects →Or pass the tests below to unlock it now.
1
The big idea
Dunder methods hook into language behaviour: repr, eq, len, iter, add, getitem, enter.
Think of it like this: Think of a Class as a cookie cutter (the blueprint) and each Object as an individual cookie stamped from it — each cookie can have its own frosting (state) while sharing the same shape (methods).
Key words (tap to learn):
2
Read the lesson
Dunder methods hook into language behaviour: __repr__, __eq__, __len__, __iter__, __add__, __getitem__, __enter__.
class Vec:
def __init__(self, x, y): self.x, self.y = x, y
def __add__(self, o): return Vec(self.x + o.x, self.y + o.y)
def __repr__(self): return f"Vec({self.x}, {self.y})"
3
Quick check
Before writing code: What should `Vec(1,2)+Vec(3,4)==Vec(4,6)` return when your implementation of Special (Dunder) Methods is working?
4
2 sample tests, 1 hiddenYour challenge
Implement Vec with __add__ and __eq__.
Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
See how Python compares side-by-side with Java.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Special (Dunder) Methods.
Mastery0%
Learn this first
- Classes and ObjectsNot yet
Opens up next
This is the last lesson in this branch.