@@ -35,52 +35,85 @@ Dictionaries are sorted by key before the display is computed.
35
35
Functions
36
36
---------
37
37
38
- .. function :: pp(object, *args, sort_dicts=False, **kwargs)
39
-
40
- Prints the formatted representation of *object * followed by a newline.
41
- If *sort_dicts * is false (the default), dictionaries will be displayed with
42
- their keys in insertion order, otherwise the dict keys will be sorted.
43
- *args * and *kwargs * will be passed to :func: `~pprint.pprint ` as formatting
44
- parameters.
45
-
46
- >>> import pprint
47
- >>> stuff = [' spam' , ' eggs' , ' lumberjack' , ' knights' , ' ni' ]
48
- >>> stuff.insert(0 , stuff)
49
- >>> pprint.pp(stuff)
50
- [<Recursion on list with id=...>,
51
- 'spam',
52
- 'eggs',
53
- 'lumberjack',
54
- 'knights',
55
- 'ni']
38
+ .. function :: pp(object, stream=None, indent=1, width=80, depth=None, *, \
39
+ compact=False, sort_dicts=False, underscore_numbers=False)
40
+
41
+ Prints the formatted representation of *object *, followed by a newline.
42
+ This function may be used in the interactive interpreter
43
+ instead of the :func: `print ` function for inspecting values.
44
+ Tip: you can reassign ``print = pprint.pp `` for use within a scope.
45
+
46
+ :param object:
47
+ The object to be printed.
48
+
49
+ :param stream:
50
+ A file-like object to which the output will be written
51
+ by calling its :meth: `!write ` method.
52
+ If ``None `` (the default), :data: `sys.stdout ` is used.
53
+ :type stream: :term: `file-like object ` | None
54
+
55
+ :param int indent:
56
+ The amount of indentation added for each nesting level.
57
+
58
+ :param int width:
59
+ The desired maximum number of characters per line in the output.
60
+ If a structure cannot be formatted within the width constraint,
61
+ a best effort will be made.
62
+
63
+ :param depth:
64
+ The number of nesting levels which may be printed.
65
+ If the data structure being printed is too deep,
66
+ the next contained level is replaced by ``... ``.
67
+ If ``None `` (the default), there is no constraint
68
+ on the depth of the objects being formatted.
69
+ :type depth: int | None
70
+
71
+ :param bool compact:
72
+ Control the way long :term: `sequences <sequence> ` are formatted.
73
+ If ``False `` (the default),
74
+ each item of a sequence will be formatted on a separate line,
75
+ otherwise as many items as will fit within the *width *
76
+ will be formatted on each output line.
77
+
78
+ :param bool sort_dicts:
79
+ If ``True ``, dictionaries will be formatted with
80
+ their keys sorted, otherwise
81
+ they will be displayed in insertion order (the default).
82
+
83
+ :param bool underscore_numbers:
84
+ If ``True ``,
85
+ integers will be formatted with the ``_ `` character for a thousands separator,
86
+ otherwise underscores are not displayed (the default).
87
+
88
+ >>> import pprint
89
+ >>> stuff = [' spam' , ' eggs' , ' lumberjack' , ' knights' , ' ni' ]
90
+ >>> stuff.insert(0 , stuff)
91
+ >>> pprint.pp(stuff)
92
+ [<Recursion on list with id=...>,
93
+ 'spam',
94
+ 'eggs',
95
+ 'lumberjack',
96
+ 'knights',
97
+ 'ni']
56
98
57
99
.. versionadded :: 3.8
58
100
59
101
60
102
.. function :: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
61
103
compact=False, sort_dicts=True, underscore_numbers=False)
62
104
63
- Prints the formatted representation of *object * on *stream *, followed by a
64
- newline. If *stream * is ``None ``, :data: `sys.stdout ` is used. This may be used
65
- in the interactive interpreter instead of the :func: `print ` function for
66
- inspecting values (you can even reassign ``print = pprint.pprint `` for use
67
- within a scope).
68
-
69
- The configuration parameters *stream *, *indent *, *width *, *depth *,
70
- *compact *, *sort_dicts * and *underscore_numbers * are passed to the
71
- :class: `PrettyPrinter ` constructor and their meanings are as
72
- described in its documentation below.
105
+ Alias for :func: `~pprint.pp ` with *sort_dicts * set to ``True `` by default,
106
+ which would automatically sort the dictionaries' keys,
107
+ you might want to use :func: `~pprint.pp ` instead where it is ``False `` by default.
73
108
74
- Note that *sort_dicts * is ``True `` by default and you might want to use
75
- :func: `~pprint.pp ` instead where it is ``False `` by default.
76
109
77
110
.. function :: pformat(object, indent=1, width=80, depth=None, *, \
78
111
compact=False, sort_dicts=True, underscore_numbers=False)
79
112
80
113
Return the formatted representation of *object * as a string. *indent *,
81
114
*width *, *depth *, *compact *, *sort_dicts * and *underscore_numbers * are
82
115
passed to the :class: `PrettyPrinter ` constructor as formatting parameters
83
- and their meanings are as described in its documentation below .
116
+ and their meanings are as described in the documentation above .
84
117
85
118
86
119
.. function :: isreadable(object)
@@ -119,51 +152,39 @@ Functions
119
152
PrettyPrinter Objects
120
153
---------------------
121
154
122
- This module defines one class:
123
-
124
- .. First the implementation class:
125
-
126
-
127
155
.. index :: single: ...; placeholder
128
156
129
157
.. class :: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
130
158
compact=False, sort_dicts=True, underscore_numbers=False)
131
159
132
- Construct a :class: `PrettyPrinter ` instance. This constructor understands
133
- several keyword parameters.
134
-
135
- *stream * (default :data: `!sys.stdout `) is a :term: `file-like object ` to
136
- which the output will be written by calling its :meth: `!write ` method.
137
- If both *stream * and :data: `!sys.stdout ` are ``None ``, then
138
- :meth: `~PrettyPrinter.pprint ` silently returns.
160
+ Construct a :class: `PrettyPrinter ` instance.
139
161
140
- Other values configure the manner in which nesting of complex data
141
- structures is displayed .
162
+ Arguments have the same meaning as for :func: ` ~pprint.pp `.
163
+ Note that they are in a different order, and that * sort_dicts * defaults to `` True `` .
142
164
143
- *indent * (default 1) specifies the amount of indentation added for
144
- each nesting level.
145
-
146
- *depth * controls the number of nesting levels which may be printed; if
147
- the data structure being printed is too deep, the next contained level
148
- is replaced by ``... ``. By default, there is no constraint on the
149
- depth of the objects being formatted.
150
-
151
- *width * (default 80) specifies the desired maximum number of characters per
152
- line in the output. If a structure cannot be formatted within the width
153
- constraint, a best effort will be made.
154
-
155
- *compact * impacts the way that long sequences (lists, tuples, sets, etc)
156
- are formatted. If *compact * is false (the default) then each item of a
157
- sequence will be formatted on a separate line. If *compact * is true, as
158
- many items as will fit within the *width * will be formatted on each output
159
- line.
160
-
161
- If *sort_dicts * is true (the default), dictionaries will be formatted with
162
- their keys sorted, otherwise they will display in insertion order.
165
+ >>> import pprint
166
+ >>> stuff = [' spam' , ' eggs' , ' lumberjack' , ' knights' , ' ni' ]
167
+ >>> stuff.insert(0 , stuff[:])
168
+ >>> pp = pprint.PrettyPrinter(indent = 4 )
169
+ >>> pp.pprint(stuff)
170
+ [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
171
+ 'spam',
172
+ 'eggs',
173
+ 'lumberjack',
174
+ 'knights',
175
+ 'ni']
176
+ >>> pp = pprint.PrettyPrinter(width = 41 , compact = True )
177
+ >>> pp.pprint(stuff)
178
+ [['spam', 'eggs', 'lumberjack',
179
+ 'knights', 'ni'],
180
+ 'spam', 'eggs', 'lumberjack', 'knights',
181
+ 'ni']
182
+ >>> tup = (' spam' , (' eggs' , (' lumberjack' , (' knights' , (' ni' , (' dead' ,
183
+ ... (' parrot' , (' fresh fruit' ,))))))))
184
+ >>> pp = pprint.PrettyPrinter(depth = 6 )
185
+ >>> pp.pprint(tup)
186
+ ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
163
187
164
- If *underscore_numbers * is true, integers will be formatted with the
165
- ``_ `` character for a thousands separator, otherwise underscores are not
166
- displayed (the default).
167
188
168
189
.. versionchanged :: 3.4
169
190
Added the *compact * parameter.
@@ -177,29 +198,6 @@ This module defines one class:
177
198
.. versionchanged :: 3.11
178
199
No longer attempts to write to :data: `!sys.stdout ` if it is ``None ``.
179
200
180
- >>> import pprint
181
- >>> stuff = [' spam' , ' eggs' , ' lumberjack' , ' knights' , ' ni' ]
182
- >>> stuff.insert(0 , stuff[:])
183
- >>> pp = pprint.PrettyPrinter(indent = 4 )
184
- >>> pp.pprint(stuff)
185
- [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
186
- 'spam',
187
- 'eggs',
188
- 'lumberjack',
189
- 'knights',
190
- 'ni']
191
- >>> pp = pprint.PrettyPrinter(width = 41 , compact = True )
192
- >>> pp.pprint(stuff)
193
- [['spam', 'eggs', 'lumberjack',
194
- 'knights', 'ni'],
195
- 'spam', 'eggs', 'lumberjack', 'knights',
196
- 'ni']
197
- >>> tup = (' spam' , (' eggs' , (' lumberjack' , (' knights' , (' ni' , (' dead' ,
198
- ... (' parrot' , (' fresh fruit' ,))))))))
199
- >>> pp = pprint.PrettyPrinter(depth = 6 )
200
- >>> pp.pprint(tup)
201
- ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
202
-
203
201
204
202
:class: `PrettyPrinter ` instances have the following methods:
205
203
0 commit comments