SY

6 min
python

reprlib - Shorter string representation of objects

Built-in repr() Function

Let's first start by understanding what the repr() function does. repr() is a built-in function that calls the __repr__ method of an object to compute the "official" string representation of the object. That is to say, it returns a string that can be used to recreate the object with the same value when passed to eval().

Check out what the standard library documentation has to say about repr() and __repr__. The repr() function is typically used for debugging and development purposes, but if you are working with large data structures or objects with long string representations, the output can be large and unwieldy, especially when reporting errors along with the stack trace, locals and function args.

The reprlib Module

reprlib (not the same as repr() or __repr__) is a Python module that provides a version of the repr() function that can be used to generate "shorter" string representations of objects. The reprlib module is especially useful when working with large data structures or objects with long string representations. The module also provides a way to customize the default limits on the size of the string representations for different data types.

In the live code examples below, a dictionary with 6 items is printed using the builtin repr() function and then using reprlib.repr() to limit the output to the first 4 items.

The reprlib module provides the following:

  1. class reprlib.Repr: A class that can be used to customize the default reprlib behavior. The functionality to pass custom limits as keyword arguments was added in Python 3.12.
  2. reprlib.aRepr: An instance of the Repr class. Attributes of this instance can be used to customize the behavior of the reprlib.repr() function.
  3. reprlib.repr(): A function that generates a shorter string representation of an object. This is a method of the aRepr instance.

Basic Usage

The reprlib module provides a function, repr(), which can be used to generate a shorter string representation of an object. The list in the following example has 10 elements, but it is truncated to the first 6 elements when printed using reprlib.repr(). The default limit for a list is 6.

basic_reprlib.py

Table 1 below shows the default limits on the size of the string representations for different data types. If the string representation of an object exceeds these limits, it will be truncated and an ellipsis ... will be added to indicate that the string has been shortened. Table 2 shows other defaults that can be customized.

Table 1: Data types and corresponding default limits
Data TypeLimit
tuple6
list6
array5
dict4
set6
frozenset6
deque6
string30
long40
other20
Table 2: Other defaults
AttributeDefaultDescription
fillvalue...The string used to indicate truncation
maxlevel6The maximum depth of nested objects

Customizing The Defaults

Custom limits for each data type can be set as attributes of the aRepr instance or passed as keyward arguments to the Repr class (added in python 3.12).

The keywords or the attributes used to customize the defaults are max followed by the name of the data type in lowercase. For example, maxdict is used to set the limit for dictionaries. The fillvalue and maxlevel attributes can also be set to customize the output.

custom_reprlib.py

Advanced Usage

@reprlib.recursive_repr decorator

The @reprlib.recursive_repr decorator can be used on the __repr__ method of a class to handle recursive calls. A fillvalue is returned if recursion is detected. Here is an example from the Python documentation:

recursive_reprlib.py

Caveats

Out In The Wild