__repr__ in Python is the special method that returns a formal text representation of an object, which is useful for debugging, logs, and checking a class’s state.
What __repr__ returns and why it matters
__repr__ in Python is meant to show an object in a way that makes it easy to identify or, in some cases, recreate in code. You usually see it in the console, inside a debugger, or when an object is displayed indirectly inside a container such as a list or dictionary.
A well-written __repr__ helps you understand what an object contains without manually inspecting every attribute. That is especially useful for classes with several fields during testing and error hunting.
How __repr__ differs from __str__
The difference between __repr__ and __str__ is their purpose: __repr__ gives a technical, precise representation, while __str__ is the readable version meant for users.
- __repr__ is for developers, debugging, logs, and diagnostics.
- __str__ is for friendly on-screen output.
- If __str__ is not defined, Python may fall back to __repr__.
A practical rule is simple: if you see an object in the console and want to understand its internal state, __repr__ is the method that matters.
How to write __repr__ in your own class
__repr__ in a class usually returns a string that includes the class name and the key attributes. That format makes the object understandable even without extra context.
Minimal example
A class with a useful __repr__ can look like this:
class User:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"User(name={self.name!r}, age={self.age!r})"
After that, calling repr(user) or viewing the object inside a list will show a clear string with its fields. The check is straightforward: if the result contains the class name and the main values, the method is working correctly. If the string is too generic, add the attributes that actually help identify the object.
What a good __repr__ should look like
A good __repr__ in Python should be short, accurate, and stable. It should not depend on random formatting or hide important data.
- Include the class name.
- Add the key attributes that define the object’s state.
- Use !r for values so strings are displayed correctly.
- Do not make __repr__ too long if the object has many fields.
For large objects, it is better to include only the fields that are truly useful for diagnostics. If __repr__ becomes bulky, it is hard to read in logs and almost impossible to compare quickly.
Common __repr__ mistakes
Common __repr__ mistakes in Python usually come from a string that is too vague or not useful enough during development.
- Returning unclear text like "Object" without details.
- Using __str__ instead of a technical representation.
- Forgetting to return a string from the method.
- Making the output so large that it clutters the console and logs.
It is safer to start with a minimal but informative version and then add only the fields that genuinely help maintain the code. If an object contains sensitive data, do not print secrets, tokens, or passwords in __repr__.
When __repr__ is especially useful
__repr__ is most helpful anywhere quick object-state inspection matters. That includes tests, logging, interactive shells, and complex nested structures built from multiple classes.
If a class is used in collections, passed between functions, or often appears in errors, a good __repr__ saves time and reduces manual checks. That is why it is worth adding to almost every custom class that may show up in debugging or logs.
