Courses/Object-Oriented Python

๐Ÿงฑ Object-Oriented Python

Dunder Methods

__str__, __eq__, __len__ โ€” make your objects feel native.

Dunder ("double underscore") methods hook into Python's syntax. Define them to make your objects work with print, ==, len, +, and more.

class Vec:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __add__(self, other):
        return Vec(self.x + other.x, self.y + other.y)
    def __repr__(self):
        return f"Vec({self.x}, {self.y})"
main.py
Output
Press Run to execute.
Expected output
(3, 4)

Sign in to track your progress across lessons.