reprlib — Alternate repr() implementation¶
Source code: Lib/reprlib.py
The reprlib module provides a means for producing object representations
with limits on the size of the resulting strings. This is used in the Python
debugger and may be useful in other contexts as well.
This module provides a class, an instance, and a function:
- class reprlib.Repr(*, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40, maxother=30, fillvalue='...', indent=None)¶
Class which provides formatting services useful in implementing functions similar to the built-in
repr(); size limits for different object types are added to avoid the generation of representations which are excessively long.The keyword arguments of the constructor can be used as a shortcut to set the attributes of the
Reprinstance. Which means that the following initialization:aRepr = reprlib.Repr(maxlevel=3)
Is equivalent to:
aRepr = reprlib.Repr() aRepr.maxlevel = 3
See section Repr Objects for more information about
Reprattributes.Changed in version 3.12: Allow attributes to be set via keyword arguments.
- reprlib.aRepr¶
This is an instance of
Reprwhich is used to provide therepr()function described below. Changing the attributes of this object will affect the size limits used byrepr()and the Python debugger.
- reprlib.repr(obj)¶
This is the
repr()method ofaRepr. It returns a string similar to that returned by the built-in function of the same name, but with limits on most sizes.
In addition to size-limiting tools, the module also provides a decorator for
detecting recursive calls to __repr__() and substituting a
placeholder string instead.
- @reprlib.recursive_repr(fillvalue='...')¶
Decorator for
__repr__()methods to detect recursive calls within the same thread. If a recursive call is made, the fillvalue is returned, otherwise, the usual__repr__()call is made. For example:>>> from reprlib import recursive_repr >>> class MyList(list): ... @recursive_repr() ... def __repr__(self): ... return '<' + '|'.join(map(repr, self)) + '>' ... >>> m = MyList('abc') >>> m.append(m) >>> m.append('x') >>> print(m) <'a'|'b'|'c'|...|'x'>
Added in version 3.2.
Repr Objects¶
Repr instances provide several attributes which can be used to provide
size limits for the representations of different object types, and methods
which format specific object types.
- Repr.fillvalue¶
This string is displayed for recursive references. It defaults to
....Added in version 3.11.
- Repr.maxlevel¶
Depth limit on the creation of recursive representations. The default is
6.
- Repr.