๐งฑ 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})"