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.
import reprlib print(reprlib.repr(list(range(100)))) # [0, 1, 2, 3, 4, 5, ...]
Try it
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.
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
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 backx. - Limits are applied per level. A list shows at most
maxlistitems, and every nested list is limited again, down tomaxlevellevels. 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
gammais missing above), and a truncated set shows the smallest items. recursive_repris per thread and per object. Without it, a__repr__that formats a container holding the object itself raisesRecursionError. The built-in containers already handle their own cycles.- Subclass
Reprfor your own types by defining a method namedrepr_<typename>(self, obj, level), and useself.repr1(x, level - 1)for nested values. - Use
pprintfor readable layout andreprlibfor short output. They solve different problems and can be combined.
Related
repr(),print(),pprint,textwrapRecursionError- Lessons: Special methods, Debugging