Python › Magic Methods & Operator Overloading

How does __repr__ help with debugging when printing a list of custom objects?

Beginner

Answer

When you print a container like a list, Python calls repr() on each element rather than str(), so a well-defined __repr__ ensures each object in a printed list, tuple, or dict is shown meaningfully instead of the default '<Point object at 0x7f...>' memory address representation.
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __repr__(self):
        return f'Point({self.x}, {self.y})'

points = [Point(1,2), Point(3,4)]
print(points)  # [Point(1, 2), Point(3, 4)]
Real-world example A debugging session inspecting a list of ORM model instances relies on each model's __repr__ showing its primary key and key fields instead of an opaque memory address.

Common follow-ups: Why does print(list_of_objects) use repr() and not str()?;What makes a 'good' __repr__ according to Python conventions?