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