10
10
11
11
--------------
12
12
13
- The :mod: `reprlib ` module provides a means for producing object representations
13
+ The :mod: `! reprlib ` module provides a means for producing object representations
14
14
with limits on the size of the resulting strings. This is used in the Python
15
15
debugger and may be useful in other contexts as well.
16
16
@@ -58,29 +58,31 @@ This module provides a class, an instance, and a function:
58
58
limits on most sizes.
59
59
60
60
In addition to size-limiting tools, the module also provides a decorator for
61
- detecting recursive calls to :meth: `__repr__ ` and substituting a placeholder
62
- string instead.
61
+ detecting recursive calls to :meth: `~object. __repr__ ` and substituting a
62
+ placeholder string instead.
63
63
64
64
65
65
.. index :: single: ...; placeholder
66
66
67
67
.. decorator :: recursive_repr(fillvalue="...")
68
68
69
- Decorator for :meth: `__repr__ ` methods to detect recursive calls within the
69
+ Decorator for :meth: `~object. __repr__ ` methods to detect recursive calls within the
70
70
same thread. If a recursive call is made, the *fillvalue * is returned,
71
- otherwise, the usual :meth: `__repr__ ` call is made. For example:
72
-
73
- >>> from reprlib import recursive_repr
74
- >>> class MyList (list ):
75
- ... @ recursive_repr()
76
- ... def __repr__ (self ):
77
- ... return ' <' + ' |' .join(map (repr , self )) + ' >'
78
- ...
79
- >>> m = MyList(' abc' )
80
- >>> m.append(m)
81
- >>> m.append(' x' )
82
- >>> print (m)
83
- <'a'|'b'|'c'|...|'x'>
71
+ otherwise, the usual :meth: `!__repr__ ` call is made. For example:
72
+
73
+ .. doctest ::
74
+
75
+ >>> from reprlib import recursive_repr
76
+ >>> class MyList (list ):
77
+ ... @ recursive_repr()
78
+ ... def __repr__ (self ):
79
+ ... return ' <' + ' |' .join(map (repr , self )) + ' >'
80
+ ...
81
+ >>> m = MyList(' abc' )
82
+ >>> m.append(m)
83
+ >>> m.append(' x' )
84
+ >>> print (m)
85
+ <'a'|'b'|'c'|...|'x'>
84
86
85
87
.. versionadded :: 3.2
86
88
@@ -148,10 +150,10 @@ which format specific object types.
148
150
with no line breaks or indentation, like the standard :func: `repr `.
149
151
For example:
150
152
151
- .. code-block :: pycon
153
+ .. doctest :: indent
152
154
153
155
>>> example = [
154
- 1, 'spam', {'a': 2, 'b': 'spam eggs', 'c': {3: 4.5, 6: []}}, 'ham']
156
+ ... 1 , ' spam' , {' a' : 2 , ' b' : ' spam eggs' , ' c' : {3 : 4.5 , 6 : []}}, ' ham' ]
155
157
>>> import reprlib
156
158
>>> aRepr = reprlib.Repr()
157
159
>>> print (aRepr.repr(example))
@@ -160,7 +162,7 @@ which format specific object types.
160
162
If :attr: `~Repr.indent ` is set to a string, each recursion level
161
163
is placed on its own line, indented by that string:
162
164
163
- .. code-block :: pycon
165
+ .. doctest :: indent
164
166
165
167
>>> aRepr.indent = ' -->'
166
168
>>> print (aRepr.repr(example))
@@ -181,7 +183,7 @@ which format specific object types.
181
183
Setting :attr: `~Repr.indent ` to a positive integer value behaves as if it
182
184
was set to a string with that number of spaces:
183
185
184
- .. code-block :: pycon
186
+ .. doctest :: indent
185
187
186
188
>>> aRepr.indent = 4
187
189
>>> print (aRepr.repr(example))
@@ -234,7 +236,9 @@ Subclassing Repr Objects
234
236
The use of dynamic dispatching by :meth: `Repr.repr1 ` allows subclasses of
235
237
:class: `Repr ` to add support for additional built-in object types or to modify
236
238
the handling of types already supported. This example shows how special support
237
- for file objects could be added::
239
+ for file objects could be added:
240
+
241
+ .. testcode ::
238
242
239
243
import reprlib
240
244
import sys
@@ -248,3 +252,7 @@ for file objects could be added::
248
252
249
253
aRepr = MyRepr()
250
254
print(aRepr.repr(sys.stdin)) # prints '<stdin>'
255
+
256
+ .. testoutput ::
257
+
258
+ <stdin>
0 commit comments