blob: 1a0c1b0dcd1aa9c04760250f4fa60f15be39b33f [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
Raymond Hettinger98b140c2011-01-23 21:05:46 +00009**Source code:** :source:`Lib/reprlib.py`
10
11--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +000013The :mod:`reprlib` module provides a means for producing object representations
Georg Brandl116aa622007-08-15 14:28:22 +000014with limits on the size of the resulting strings. This is used in the Python
15debugger and may be useful in other contexts as well.
16
17This module provides a class, an instance, and a function:
18
19
20.. class:: Repr()
21
22 Class which provides formatting services useful in implementing functions
23 similar to the built-in :func:`repr`; size limits for different object types
24 are added to avoid the generation of representations which are excessively long.
25
26
27.. data:: aRepr
28
Georg Brandl502d9a52009-07-26 15:02:41 +000029 This is an instance of :class:`Repr` which is used to provide the
30 :func:`.repr` function described below. Changing the attributes of this
31 object will affect the size limits used by :func:`.repr` and the Python
32 debugger.
Georg Brandl116aa622007-08-15 14:28:22 +000033
34
35.. function:: repr(obj)
36
Georg Brandl502d9a52009-07-26 15:02:41 +000037 This is the :meth:`~Repr.repr` method of ``aRepr``. It returns a string
38 similar to that returned by the built-in function of the same name, but with
39 limits on most sizes.
Georg Brandl116aa622007-08-15 14:28:22 +000040
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000041In addition to size-limiting tools, the module also provides a decorator for
42detecting recursive calls to :meth:`__repr__` and substituting a placeholder
43string instead.
44
Serhiy Storchaka6c48bf22018-11-20 19:26:09 +020045
46.. index:: single: ...; placeholder
47
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000048.. decorator:: recursive_repr(fillvalue="...")
49
50 Decorator for :meth:`__repr__` methods to detect recursive calls within the
51 same thread. If a recursive call is made, the *fillvalue* is returned,
52 otherwise, the usual :meth:`__repr__` call is made. For example:
53
Marco Buttue65fcde2017-04-27 14:23:34 +020054 >>> from reprlib import recursive_repr
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000055 >>> class MyList(list):
56 ... @recursive_repr()
Raymond Hettinger400daed2014-08-02 22:32:10 -070057 ... def __repr__(self):
58 ... return '<' + '|'.join(map(repr, self)) + '>'
Raymond Hettinger98a5f3f2010-09-13 21:36:00 +000059 ...
60 >>> m = MyList('abc')
61 >>> m.append(m)
62 >>> m.append('x')
63 >>> print(m)
64 <'a'|'b'|'c'|...|'x'>
65
66 .. versionadded:: 3.2
67
Georg Brandl116aa622007-08-15 14:28:22 +000068
69.. _repr-objects:
70
71Repr Objects
72------------
73
Senthil Kumarana6bac952011-07-04 11:28:30 -070074:class:`Repr` instances provide several attributes which can be used to provide
Georg Brandl116aa622007-08-15 14:28:22 +000075size limits for the representations of different object types, and methods
76which format specific object types.
77
78
79.. attribute:: Repr.maxlevel
80
81 Depth limit on the creation of recursive representations. The default is ``6``.
82
83
84.. attribute:: Repr.maxdict
85 Repr.maxlist
86 Repr.maxtuple
87 Repr.maxset
88 Repr.maxfrozenset
89 Repr.maxdeque
90 Repr.maxarray
91
92 Limits on the number of entries represented for the named object type. The
93 default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
94 the others.
95
Georg Brandl116aa622007-08-15 14:28:22 +000096
97.. attribute:: Repr.maxlong
98
Mark Dickinsonbf5c6a92009-01-17 10:21:23 +000099 Maximum number of characters in the representation for an integer. Digits
Georg Brandl116aa622007-08-15 14:28:22 +0000100 are dropped from the middle. The default is ``40``.
101
102
103.. attribute:: Repr.maxstring
104
105 Limit on the number of characters in the representation of the string. Note
106 that the "normal" representation of the string is used as the character source:
107 if escape sequences are needed in the representation, these may be mangled when
108 the representation is shortened. The default is ``30``.
109
110
111.. attribute:: Repr.maxother
112
113 This limit is used to control the size of object types for which no specific
114 formatting method is available on the :class:`Repr` object. It is applied in a
115 similar manner as :attr:`maxstring`. The default is ``20``.
116
117
118.. method:: Repr.repr(obj)
119
120 The equivalent to the built-in :func:`repr` that uses the formatting imposed by
121 the instance.
122
123
124.. method:: Repr.repr1(obj, level)
125
Georg Brandl502d9a52009-07-26 15:02:41 +0000126 Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
Georg Brandl116aa622007-08-15 14:28:22 +0000127 determine which formatting method to call, passing it *obj* and *level*. The
128 type-specific methods should call :meth:`repr1` to perform recursive formatting,
129 with ``level - 1`` for the value of *level* in the recursive call.
130
131
132.. method:: Repr.repr_TYPE(obj, level)
133 :noindex:
134
135 Formatting methods for specific types are implemented as methods with a name
136 based on the type name. In the method name, **TYPE** is replaced by
Berker Peksag2d510e32014-09-18 06:05:14 +0300137 ``'_'.join(type(obj).__name__.split())``. Dispatch to these methods is
138 handled by :meth:`repr1`. Type-specific methods which need to recursively
139 format a value should call ``self.repr1(subobj, level - 1)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
142.. _subclassing-reprs:
143
144Subclassing Repr Objects
145------------------------
146
147The use of dynamic dispatching by :meth:`Repr.repr1` allows subclasses of
148:class:`Repr` to add support for additional built-in object types or to modify
149the handling of types already supported. This example shows how special support
150for file objects could be added::
151
Benjamin Petersonaa306692008-10-15 03:09:45 +0000152 import reprlib
Georg Brandl116aa622007-08-15 14:28:22 +0000153 import sys
154
Benjamin Petersonaa306692008-10-15 03:09:45 +0000155 class MyRepr(reprlib.Repr):
Berker Peksagedd6ec22014-10-12 05:11:16 +0300156
157 def repr_TextIOWrapper(self, obj, level):
158 if obj.name in {'<stdin>', '<stdout>', '<stderr>'}:
Georg Brandl116aa622007-08-15 14:28:22 +0000159 return obj.name
Berker Peksagedd6ec22014-10-12 05:11:16 +0300160 return repr(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000161
162 aRepr = MyRepr()
Georg Brandlede6c2a2010-01-05 10:22:04 +0000163 print(aRepr.repr(sys.stdin)) # prints '<stdin>'