repr()
FunctionLet'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.
reprlib
Modulereprlib
(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:
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.reprlib.aRepr
: An instance of the Repr
class. Attributes of this instance can be
used to customize the behavior of the reprlib.repr()
function.reprlib.repr()
: A function that generates a shorter string representation of an
object. This is a method of the aRepr
instance.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.
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.
Data Type | Limit |
---|---|
tuple | 6 |
list | 6 |
array | 5 |
dict | 4 |
set | 6 |
frozenset | 6 |
deque | 6 |
string | 30 |
long | 40 |
other | 20 |
Attribute | Default | Description |
---|---|---|
fillvalue | ... | The string used to indicate truncation |
maxlevel | 6 | The maximum depth of nested objects |
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.
@reprlib.recursive_repr
decoratorThe @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:
reprlib
based on the limits set and the structure of the object being represented. You can
adjust the limits to suit your needs.