__repr__Easy Examples

Returns an unambiguous developer-facing string representation

Implementing __repr__

Basic implementation of __repr__ in a class.

python
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f"Point({self.x}, {self.y})"

p = Point(3, 4)
print(repr(p))
print(p)  # Falls back to __repr__ if no __str__

__repr__ returns an unambiguous developer-facing string representation. Implementing it lets you customize how Python interacts with your objects.

__repr__ in action

Seeing __repr__ called by Python's built-in operations.

python
# How Python calls __repr__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        print(f"__repr__ was called!")
        return f"Demo({self.value})"

d = Demo(42)
# This triggers __repr__:
print(repr(d))

Python automatically calls __repr__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground