Special Methods
Dunder methods customise objects: __str__, __repr__, __len__, __eq__, __add__ and __iter__.
Example
class V:
def __init__(self, x): self.x = x
def __add__(self, o): return V(self.x + o.x)
def __len__(self): return self.x
def __str__(self): return f"V({self.x})"
print(V(2) + V(3), len(V(4)))Result
V(5) 4
Exercise
Text of an object.
def (self):
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.