blob: c794a8d389cda39e4eb869b11ef45d1aba4d778e [file] [log] [blame]
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +00001:mod:`reprlib` --- Alternate :func:`repr` implementation
Alexandre Vassalottibee32532008-05-16 18:15:12 +00002========================================================
3
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +00004.. module:: reprlib
Georg Brandl116aa622007-08-15 14:28:22 +00005 :synopsis: Alternate repr() implementation with size limits.
6.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
7
8
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +00009The :mod:`reprlib` module provides a means for producing object representations
Georg Brandl116aa622007-08-15 14:28:22 +000010with limits on the size of the resulting strings. This is used in the Python
11debugger and may be useful in other contexts as well.
12
13This module provides a class, an instance, and a function:
14
15
16.. class:: Repr()
17
18 Class which provides formatting services useful in implementing functions
19 similar to the built-in :func:`repr`; size limits for different object types
20 are added to avoid the generation of representations which are excessively long.
21
22
23.. data:: aRepr
24
Georg Brandl502d9a52009-07-26 15:02:41 +000025 This is an instance of :class:`Repr` which is used to provide the
26 :func:`.repr` function described below. Changing the attributes of this
27 object will affect the size limits used by :func:`.repr` and the Python
28 debugger.
Georg Brandl116aa622007-08-15 14:28:22 +000029
30
31.. function:: repr(obj)
32
Georg Brandl502d9a52009-07-26 15:02:41 +000033 This is the :meth:`~Repr.repr` method of ``aRepr``. It returns a string
34 similar to that returned by the built-in function of the same name, but with
35 limits on most sizes.
Georg Brandl116aa622007-08-15 14:28:22 +000036
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000037In addition to size-limiting tools, the module also provides a decorator for
38detecting recursive calls to :meth:`__repr__` and substituting a placeholder
39string instead.
40
41.. decorator:: recursive_repr(fillvalue="...")
42
43 Decorator for :meth:`__repr__` methods to detect recursive calls within the
44 same thread. If a recursive call is made, the *fillvalue* is returned,
45 otherwise, the usual :meth:`__repr__` call is made. For example:
46
47 >>> class MyList(list):
48 ... @recursive_repr()
49 ... def __repr__(self):
50 ... return '<' + '|'.join(map(repr, self)) + '>'
51 ...
52 >>> m = MyList('abc')
53 >>> m.append(m)
54 >>> m.append('x')
55 >>> print(m)
56 <'a'|'b'|'c'|...|'x'>
57
58 .. versionadded:: 3.2
59
Georg Brandl116aa622007-08-15 14:28:22 +000060
61.. _repr-objects:
62
63Repr Objects
64------------
65
66:class:`Repr` instances provide several members which can be used to provide
67size limits for the representations of different object types, and methods
68which format specific object types.
69
70
71.. attribute:: Repr.maxlevel
72
73 Depth limit on the creation of recursive representations. The default is ``6``.
74
75
76.. attribute:: Repr.maxdict
77 Repr.maxlist
78 Repr.maxtuple
79 Repr.maxset
80 Repr.maxfrozenset
81 Repr.maxdeque
82 Repr.maxarray
83
84 Limits on the number of entries represented for the named object type. The
85 default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
86 the others.
87
Georg Brandl116aa622007-08-15 14:28:22 +000088
89.. attribute:: Repr.maxlong
90
Mark Dickinsonbf5c6a92009-01-17 10:21:23 +000091 Maximum number of characters in the representation for an integer. Digits
Georg Brandl116aa622007-08-15 14:28:22 +000092 are dropped from the middle. The default is ``40``.
93
94
95.. attribute:: Repr.maxstring
96
97 Limit on the number of characters in the representation of the string. Note
98 that the "normal" representation of the string is used as the character source:
99 if escape sequences are needed in the representation, these may be mangled when
100 the representation is shortened. The default is ``30``.
101
102
103.. attribute:: Repr.maxother
104
105 This limit is used to control the size of object types for which no specific
106 formatting method is available on the :class:`Repr` object. It is applied in a
107 similar manner as :attr:`maxstring`. The default is ``20``.
108
109
110.. method:: Repr.repr(obj)
111
112 The equivalent to the built-in :func:`repr` that uses the formatting imposed by
113 the instance.
114
115
116.. method:: Repr.repr1(obj, level)
117
Georg Brandl502d9a52009-07-26 15:02:41 +0000118 Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
Georg Brandl116aa622007-08-15 14:28:22 +0000119 determine which formatting method to call, passing it *obj* and *level*. The
120 type-specific methods should call :meth:`repr1` to perform recursive formatting,
121 with ``level - 1`` for the value of *level* in the recursive call.
122
123
124.. method:: Repr.repr_TYPE(obj, level)
125 :noindex:
126
127 Formatting methods for specific types are implemented as methods with a name
128 based on the type name. In the method name, **TYPE** is replaced by
129 ``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
130 methods is handled by :meth:`repr1`. Type-specific methods which need to
131 recursively format a value should call ``self.repr1(subobj, level - 1)``.
132
133
134.. _subclassing-reprs:
135
136Subclassing Repr Objects
137------------------------
138
139The use of dynamic dispatching by :meth:`Repr.repr1` allows subclasses of
140:class:`Repr` to add support for additional built-in object types or to modify
141the handling of types already supported. This example shows how special support
142for file objects could be added::
143
Benjamin Petersonaa306692008-10-15 03:09:45 +0000144 import reprlib
Georg Brandl116aa622007-08-15 14:28:22 +0000145 import sys
146
Benjamin Petersonaa306692008-10-15 03:09:45 +0000147 class MyRepr(reprlib.Repr):
Georg Brandl116aa622007-08-15 14:28:22 +0000148 def repr_file(self, obj, level):
149 if obj.name in ['<stdin>', '<stdout>', '<stderr>']:
150 return obj.name
151 else:
Georg Brandl0df79792008-10-04 18:33:26 +0000152 return repr(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154 aRepr = MyRepr()
Georg Brandlede6c2a2010-01-05 10:22:04 +0000155 print(aRepr.repr(sys.stdin)) # prints '<stdin>'
Georg Brandl116aa622007-08-15 14:28:22 +0000156