Python repr() Function

Spread the love

In Python, repr() is a built-in function used to obtain the developer-friendly string representation of an object. The string returned by repr() is generally unambiguous and, ideally, can be used to reproduce the object when fed back to the Python interpreter.


The syntax of repr() is straightforward:



  • obj – The object whose string representation you want to get.

Return Value:

The function returns a string that theoretically should be a valid Python expression that could be used to recreate the object.

Understanding the Purpose of repr()

The primary purpose of repr() is to produce a string representation of an object that is more geared towards developers rather than end-users. It is often said that repr() is for developers’ eyes. It provides insights into the object that can be crucial for debugging.


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

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

p = Point(1, 2)
print(repr(p))  # Output: Point(1, 2)

repr() vs str()

In Python, objects can define two methods that control how they are converted to strings: __repr__() and __str__(). The repr() function uses the former, __repr__(), while the str() function uses the latter, __str__().

__str__( ) :

  • Aimed at end-users.
  • Should return a friendly or nicely printable string.
  • Used by the built-in function str() and by the print() function.

__repr__( ) :

  • Aimed at developers.
  • Should return an “official” string representation of an object.
  • If possible, it should look like a valid Python expression that could be used to recreate the object.
  • Used by the built-in function repr() and by backticks (in Python 2.x).

If an object does not have __str__ implemented, Python falls back to using __repr__ for the built-in function str() as well.

Implementing Custom repr() Representations

In custom classes, developers can define their own __repr__ method. This method should return a string containing a printable representation of the object.

Example of a Custom __repr__ :

class Employee:
    def __init__(self, name, id): = name = id

    def __repr__(self):
        return f"Employee('{}', {})"

emp = Employee('Bhola Prasad', 12345)
print(repr(emp))  # Employee('Bhola Prasad', 12345)

In this example, calling repr(emp) returns a string that resembles a constructor call to recreate the Employee object.

Usage of repr() in Built-in Types

Python’s built-in types already have a __repr__ implementation that usually returns the code representation of the object. For example, for a list or dictionary, repr() will return a string that, if copy-pasted back into Python, would yield an equivalent list or dictionary.

my_list = [1, 2, 3]
my_dict = {'apple': 'red', 'banana': 'yellow'}

print(repr(my_list))  # Output: [1, 2, 3]
print(repr(my_dict))  # Output: {'apple': 'red', 'banana': 'yellow'}

repr() for Debugging

repr() becomes exceptionally useful when logging or debugging. The detailed and unambiguous string representation it provides can be crucial for understanding the state and type of the object at a specific point in execution.

Example with Debugging:

import logging


a = [1, 2, 3]
b = {'key': 'value'}

logging.debug("List a: %r", a)
logging.debug("Dictionary b: %r", b)

This will log the repr() of the objects, which helps in getting the detailed structure of the objects in the debug output.

The reprlib Module

For containers containing a large number of elements, the default repr() might generate a very long string. The reprlib module provides a means to produce a shortened string representation of large containers.

Example of Using reprlib:

import reprlib

large_list = list(range(100))
print(reprlib.repr(large_list))  # Output: [0, 1, 2, 3, 4, 5, ...]

Here, reprlib.repr gives us a shortened version of the list representation.

Edge Cases and Considerations

While the repr() function is incredibly useful, it has some edge cases and considerations that developers should be aware of:

  • Not all objects can be “round-tripped” (recreated from their repr() string).
  • Security considerations: Be cautious of executing the result of repr() if the object representation includes user input or data from an untrusted source.
  • Performance: For very large objects, calling repr() can be expensive, as it generates a string representation for the entire object.


The repr() function is an indispensable aspect of Python development, particularly when it comes to debugging and logging. It offers a developer-oriented string representation of objects that can be invaluable in understanding their structure and state. By customizing __repr__, developers can control what details are conveyed when their objects are printed or logged, which can significantly enhance the maintainability and transparency of the code.

Leave a Reply