Complete guide to Python's __repr__ method covering string representation, debugging, and best practices.
Last modified April 8, 2025
This comprehensive guide explores Python’s repr method, the special method that returns the official string representation of an object.
The repr method returns a string representation of an object. It is called by the repr() built-in function and should be unambiguous.
A good repr should look like a valid Python expression that could recreate the object. It’s primarily used for debugging and development.
Here’s a simple implementation showing how repr works with a basic class. The method should return a string.
basic_repr.py
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)) # Output: Point(3, 4)
This example shows a proper repr implementation. The output looks like a valid Python expression that could recreate the object.
When you print the object directly or use repr(), Python calls this method to get the string representation.
repr and str serve different purposes. repr is for developers while str is for end users.
repr_vs_str.py
class Book: def init(self, title, author): self.title = title self.author = author
def __repr__(self):
return f"Book('{self.title}', '{self.author}')"
def __str__(self):
return f"{self.title} by {self.author}"
book = Book(“Python Crash Course”, “Eric Matthes”) print(repr(book)) # Book(‘Python Crash Course’, ‘Eric Matthes’) print(str(book)) # Python Crash Course by Eric Matthes
This example demonstrates the difference. repr shows how to recreate the object while str provides a user-friendly string.
If str isn’t defined, Python will use repr as a fallback.
A good repr is invaluable for debugging as it shows the exact state of an object. Here’s an example with a more complex class.
debugging_repr.py
class ShoppingCart: def init(self): self.items = []
def add_item(self, product, quantity):
self.items.append((product, quantity))
def __repr__(self):
items_repr = ', '.join(
f"('{p}', {q})" for p, q in self.items
)
return f"ShoppingCart([{items_repr}])"
cart = ShoppingCart() cart.add_item(“Apple”, 3) cart.add_item(“Banana”, 5) print(repr(cart))
This repr shows the complete state of the shopping cart, including all items and their quantities. It’s perfect for debugging.
The representation includes nested structures and maintains the exact format that could be used to recreate the object.
When using inheritance, you should include the class name and all relevant attributes in repr. Here’s how to do it properly.
inheritance_repr.py
class Person: def init(self, name, age): self.name = name self.age = age
def __repr__(self):
return f"{self.__class__.__name__}({self.name!r}, {self.age})"
class Employee(Person): def init(self, name, age, employee_id): super().init(name, age) self.employee_id = employee_id
def __repr__(self):
return (f"{self.__class__.__name__}({self.name!r}, "
f"{self.age}, {self.employee_id!r})")
emp = Employee(“John Doe”, 30, “E12345”) print(repr(emp)) # Employee(‘John Doe’, 30, ‘E12345’)
This example shows proper repr implementation with inheritance. The child class includes all attributes from both parent and child.
Using self.class.name makes the representation work correctly even with subclassing.
For container-like classes, repr should mimic Python’s built-in containers. Here’s an example with a custom list class.
container_repr.py
class TaggedList: def init(self, tag, *items): self.tag = tag self.items = list(items)
def __repr__(self):
items_repr = ', '.join(repr(item) for item in self.items)
return f"{self.__class__.__name__}({self.tag!r}, {items_repr})"
def __str__(self):
return f"{self.tag}: {', '.join(str(item) for item in self.items)}"
tags = TaggedList(“Numbers”, 1, 2, 3, 4) print(repr(tags)) # TaggedList(‘Numbers’, 1, 2, 3, 4)
This repr follows Python’s convention for container types, showing the class name and all contained elements in a comma-separated list.
The representation could be used to recreate the object exactly as it was, which is the primary goal of repr.
Be unambiguous: The representation should clearly identify the object
Make it executable: Ideally, eval(repr(obj)) should recreate the object
Include all state: Don’t omit important attributes
Use the class name: Helps identify the object type
Keep it readable: Balance completeness with readability
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.