1 :mod:`repr` --- Alternate :func:`repr` implementation
2 =====================================================
5 :synopsis: Alternate repr() implementation with size limits.
6 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9 The :mod:`repr` module has been renamed to :mod:`reprlib` in Python 3.0. The
10 :term:`2to3` tool will automatically adapt imports when converting your
13 **Source code:** :source:`Lib/repr.py`
17 The :mod:`repr` module provides a means for producing object representations
18 with limits on the size of the resulting strings. This is used in the Python
19 debugger and may be useful in other contexts as well.
21 This module provides a class, an instance, and a function:
26 Class which provides formatting services useful in implementing functions
27 similar to the built-in :func:`repr`; size limits for different object types
28 are added to avoid the generation of representations which are excessively long.
33 This is an instance of :class:`Repr` which is used to provide the :func:`.repr`
34 function described below. Changing the attributes of this object will affect
35 the size limits used by :func:`.repr` and the Python debugger.
38 .. function:: repr(obj)
40 This is the :meth:`~Repr.repr` method of ``aRepr``. It returns a string
41 similar to that returned by the built-in function of the same name, but with
50 :class:`Repr` instances provide several attributes which can be used to provide
51 size limits for the representations of different object types, and methods
52 which format specific object types.
55 .. attribute:: Repr.maxlevel
57 Depth limit on the creation of recursive representations. The default is ``6``.
60 .. attribute:: Repr.maxdict
68 Limits on the number of entries represented for the named object type. The
69 default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
73 :attr:`maxset`, :attr:`maxfrozenset`, and :attr:`set`.
76 .. attribute:: Repr.maxlong
78 Maximum number of characters in the representation for a long integer. Digits
79 are dropped from the middle. The default is ``40``.
82 .. attribute:: Repr.maxstring
84 Limit on the number of characters in the representation of the string. Note
85 that the "normal" representation of the string is used as the character source:
86 if escape sequences are needed in the representation, these may be mangled when
87 the representation is shortened. The default is ``30``.
90 .. attribute:: Repr.maxother
92 This limit is used to control the size of object types for which no specific
93 formatting method is available on the :class:`Repr` object. It is applied in a
94 similar manner as :attr:`maxstring`. The default is ``20``.
97 .. method:: Repr.repr(obj)
99 The equivalent to the built-in :func:`repr` that uses the formatting imposed by
103 .. method:: Repr.repr1(obj, level)
105 Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
106 determine which formatting method to call, passing it *obj* and *level*. The
107 type-specific methods should call :meth:`repr1` to perform recursive formatting,
108 with ``level - 1`` for the value of *level* in the recursive call.
111 .. method:: Repr.repr_TYPE(obj, level)
114 Formatting methods for specific types are implemented as methods with a name
115 based on the type name. In the method name, **TYPE** is replaced by
116 ``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
117 methods is handled by :meth:`repr1`. Type-specific methods which need to
118 recursively format a value should call ``self.repr1(subobj, level - 1)``.
121 .. _subclassing-reprs:
123 Subclassing Repr Objects
124 ------------------------
126 The use of dynamic dispatching by :meth:`Repr.repr1` allows subclasses of
127 :class:`Repr` to add support for additional built-in object types or to modify
128 the handling of types already supported. This example shows how special support
129 for file objects could be added::
131 import repr as reprlib
134 class MyRepr(reprlib.Repr):
135 def repr_file(self, obj, level):
136 if obj.name in ['<stdin>', '<stdout>', '<stderr>']:
142 print aRepr.repr(sys.stdin) # prints '<stdin>'