Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Standard Library: Data Structures and Functional Tools

The reprlib module

Reference for Python's reprlib module: repr(), the Repr class, size limits such as maxlist and maxstring, and @recursive_repr for self-referencing objects.

The reprlib module provides a version of repr() that cuts long output short. The built-in repr() of a list with a million items produces a million items of text, which floods a terminal or log file. reprlib.repr() shows only the first few elements and puts ... where it left things out.

It is most useful in debugging helpers, interactive tools, logging and in the __repr__ methods of your own container classes. The limits are configurable per type, and a decorator, recursive_repr, protects __repr__ methods from infinite recursion when an object contains itself.

Python
import reprlib

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

Try it

Python Shortened output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Custom limits and recursive_repr

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
reprlib.repr(obj)
reprlib.Repr()
reprlib.recursive_repr(fillvalue="...")
Name Description
reprlib.repr(obj) Returns a size-limited string for obj using the module's shared Repr instance (reprlib.aRepr).
Repr Class that does the work. Create your own instance, change its attributes, and call its repr(obj) method.
recursive_repr(fillvalue="...") Decorator for __repr__ that returns fillvalue if the method is called again for the same object in the same thread.

Useful Repr attributes: maxlevel (nesting depth), maxlist, maxtuple, maxset, maxfrozenset, maxdeque, maxarray, maxdict (item counts), maxstring and maxother (characters), maxlong (digits of an integer), and indent (3.12) for multi-line output.

Return value: a str.

Usage notes

  • The output is for people. Because it drops items, eval(reprlib.repr(x)) generally does not give back x.
  • Limits are applied per level. A list shows at most maxlist items, and every nested list is limited again, down to maxlevel levels. Deeper levels appear as [...].
  • Strings keep both ends. When a string is too long, the middle is replaced by ... so you see how it starts and ends.
  • Dicts and sets are sorted when possible. A truncated dict shows the first keys in sorted order (not insertion order, which is why gamma is missing above), and a truncated set shows the smallest items.
  • recursive_repr is per thread and per object. Without it, a __repr__ that formats a container holding the object itself raises RecursionError. The built-in containers already handle their own cycles.
  • Subclass Repr for your own types by defining a method named repr_<typename>(self, obj, level), and use self.repr1(x, level - 1) for nested values.
  • Use pprint for readable layout and reprlib for short output. They solve different problems and can be combined.