--------------
-The :mod:`reprlib` module provides a means for producing object representations
+The :mod:`!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.
limits on most sizes.
In addition to size-limiting tools, the module also provides a decorator for
-detecting recursive calls to :meth:`__repr__` and substituting a placeholder
-string instead.
+detecting recursive calls to :meth:`~object.__repr__` and substituting a
+placeholder string instead.
.. index:: single: ...; placeholder
.. decorator:: recursive_repr(fillvalue="...")
- Decorator for :meth:`__repr__` methods to detect recursive calls within the
+ Decorator for :meth:`~object.__repr__` methods to detect recursive calls within the
same thread. If a recursive call is made, the *fillvalue* is returned,
- otherwise, the usual :meth:`__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'>
+ otherwise, the usual :meth:`!__repr__` call is made. For example:
+
+ .. doctest::
+
+ >>> 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'>
.. versionadded:: 3.2
with no line breaks or indentation, like the standard :func:`repr`.
For example:
- .. code-block:: pycon
+ .. doctest:: indent
>>> example = [
- 1, 'spam', {'a': 2, 'b': 'spam eggs', 'c': {3: 4.5, 6: []}}, 'ham']
+ ... 1, 'spam', {'a': 2, 'b': 'spam eggs', 'c': {3: 4.5, 6: []}}, 'ham']
>>> import reprlib
>>> aRepr = reprlib.Repr()
>>> print(aRepr.repr(example))
If :attr:`~Repr.indent` is set to a string, each recursion level
is placed on its own line, indented by that string:
- .. code-block:: pycon
+ .. doctest:: indent
>>> aRepr.indent = '-->'
>>> print(aRepr.repr(example))
Setting :attr:`~Repr.indent` to a positive integer value behaves as if it
was set to a string with that number of spaces:
- .. code-block:: pycon
+ .. doctest:: indent
>>> aRepr.indent = 4
>>> print(aRepr.repr(example))
The use of dynamic dispatching by :meth:`Repr.repr1` allows subclasses of
:class:`Repr` to add support for additional built-in object types or to modify
the handling of types already supported. This example shows how special support
-for file objects could be added::
+for file objects could be added:
+
+.. testcode::
import reprlib
import sys
aRepr = MyRepr()
print(aRepr.repr(sys.stdin)) # prints '<stdin>'
+
+.. testoutput::
+
+ <stdin>