Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

__repr__

__repr__ defines an object's official string representation for repr(). Make it information-rich and unambiguous, ideally resembling Python code that can recreate the value.

How it works

Define __repr__(self) to return the string produced when repr() represents an instance. Prefer a clear, information-rich result that resembles a Python expression able to recreate the value; if that is not feasible, return a useful angle-bracket description. When __str__ is absent, Python also uses this method for informal string conversion.

Syntax

object.__repr__(self)

Parameters

NameTypeDescription
selfobjectThe instance whose official representation is being computed.

Returns

A string object containing the object's official representation.

Key points

  • Python invokes __repr__ through repr() to obtain an object's official textual representation.
  • A strong representation resembles executable Python that could reconstruct an equivalent object in the right environment.
  • When reconstruction syntax is unavailable, return a useful angle-bracket description instead.
  • Optimize the output for debugging by including enough detail to identify the object's state without ambiguity.
  • Classes that do not customize the method inherit a default implementation from object.

Common mistakes

  • Returning a non-string value from __repr__ is invalid and raises an error when the representation is requested.
  • Defining only __repr__ also controls the class's informal string representation, so it may appear in contexts where a concise display was expected.

Example: A useful object representation

class User:
    def __init__(self, name, role):
        self.name = name
        self.role = role

    def __repr__(self):
        return f"User(name={self.name!r}, role={self.role!r})"


user = User("Ada", "admin")
print(repr(user))

The method returns a readable representation containing constructor-like field values.

When to use it

Debugging state

Inspecting objects in a debugger or interactive session.

Diagnostic logging

Logging domain objects with identifying fields.

Reconstructible display

Producing constructor-like output for value-oriented classes.

Interview questions

QWhat is __repr__ used for?

__repr__ is called by repr() to compute an object's official string representation. It should be information-rich and unambiguous, ideally resembling a valid Python expression that can recreate the value.

Follow-up: What should it return if recreation syntax is impractical? Return a string shaped like <...some useful description...>.

QWhat must __repr__ return?

The method's result must be a string. A class can implement it as def __repr__(self), and the built-in repr() invokes it for instances.

Follow-up: What happens if the method returns an integer? It violates the required return type and representation fails.

QWhat is the relationship between __repr__ and __str__?

If __str__ is not defined, Python uses __repr__ when an informal string representation is needed. This makes a sole __repr__ implementation affect both debugging output and informal display.

Follow-up: How does this differ from __str__? __str__ has no expectation of returning a valid Python expression and may be more convenient or concise.

  • repr()
  • object.__str__()
  • str()
  • object

Source: 3. Data model — Python 3.15.0 documentation. Checked 2026-10-16.