2 :mod:`pprint` --- Data pretty printer
3 =====================================
6 :synopsis: Data pretty printer.
7 .. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
11 The :mod:`pprint` module provides a capability to "pretty-print" arbitrary
12 Python data structures in a form which can be used as input to the interpreter.
13 If the formatted structures include objects which are not fundamental Python
14 types, the representation may not be loadable. This may be the case if objects
15 such as files, sockets, classes, or instances are included, as well as many
16 other built-in objects which are not representable as Python constants.
18 The formatted representation keeps objects on a single line if it can, and
19 breaks them onto multiple lines if they don't fit within the allowed width.
20 Construct :class:`PrettyPrinter` objects explicitly if you need to adjust the
23 .. versionchanged:: 2.5
24 Dictionaries are sorted by key before the display is computed; before 2.5, a
25 dictionary was sorted only if its display required more than one line, although
26 that wasn't documented.
28 .. versionchanged:: 2.6
29 Added support for :class:`set` and :class:`frozenset`.
33 Latest version of the `pprint module Python source code
34 <http://svn.python.org/view/python/branches/release27-maint/Lib/pprint.py?view=markup>`_
36 The :mod:`pprint` module defines one class:
38 .. First the implementation class:
41 .. class:: PrettyPrinter(...)
43 Construct a :class:`PrettyPrinter` instance. This constructor understands
44 several keyword parameters. An output stream may be set using the *stream*
45 keyword; the only method used on the stream object is the file protocol's
46 :meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts
47 ``sys.stdout``. Three additional parameters may be used to control the
48 formatted representation. The keywords are *indent*, *depth*, and *width*. The
49 amount of indentation added for each recursive level is specified by *indent*;
50 the default is one. Other values can cause output to look a little odd, but can
51 make nesting easier to spot. The number of levels which may be printed is
52 controlled by *depth*; if the data structure being printed is too deep, the next
53 contained level is replaced by ``...``. By default, there is no constraint on
54 the depth of the objects being formatted. The desired output width is
55 constrained using the *width* parameter; the default is 80 characters. If a
56 structure cannot be formatted within the constrained width, a best effort will
60 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
61 >>> stuff.insert(0, stuff[:])
62 >>> pp = pprint.PrettyPrinter(indent=4)
64 [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
70 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
71 ... ('parrot', ('fresh fruit',))))))))
72 >>> pp = pprint.PrettyPrinter(depth=6)
74 ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
76 The :class:`PrettyPrinter` class supports several derivative functions:
78 .. Now the derivative functions:
80 .. function:: pformat(object[, indent[, width[, depth]]])
82 Return the formatted representation of *object* as a string. *indent*, *width*
83 and *depth* will be passed to the :class:`PrettyPrinter` constructor as
84 formatting parameters.
86 .. versionchanged:: 2.4
87 The parameters *indent*, *width* and *depth* were added.
90 .. function:: pprint(object[, stream[, indent[, width[, depth]]]])
92 Prints the formatted representation of *object* on *stream*, followed by a
93 newline. If *stream* is omitted, ``sys.stdout`` is used. This may be used in
94 the interactive interpreter instead of a :keyword:`print` statement for
95 inspecting values. *indent*, *width* and *depth* will be passed to the
96 :class:`PrettyPrinter` constructor as formatting parameters.
99 >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
100 >>> stuff.insert(0, stuff)
101 >>> pprint.pprint(stuff)
102 [<Recursion on list with id=...>,
109 .. versionchanged:: 2.4
110 The parameters *indent*, *width* and *depth* were added.
113 .. function:: isreadable(object)
115 .. index:: builtin: eval
117 Determine if the formatted representation of *object* is "readable," or can be
118 used to reconstruct the value using :func:`eval`. This always returns ``False``
119 for recursive objects.
121 >>> pprint.isreadable(stuff)
125 .. function:: isrecursive(object)
127 Determine if *object* requires a recursive representation.
130 One more support function is also defined:
132 .. function:: saferepr(object)
134 Return a string representation of *object*, protected against recursive data
135 structures. If the representation of *object* exposes a recursive entry, the
136 recursive reference will be represented as ``<Recursion on typename with
137 id=number>``. The representation is not otherwise formatted.
139 >>> pprint.saferepr(stuff)
140 "[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
143 .. _prettyprinter-objects:
145 PrettyPrinter Objects
146 ---------------------
148 :class:`PrettyPrinter` instances have the following methods:
151 .. method:: PrettyPrinter.pformat(object)
153 Return the formatted representation of *object*. This takes into account the
154 options passed to the :class:`PrettyPrinter` constructor.
157 .. method:: PrettyPrinter.pprint(object)
159 Print the formatted representation of *object* on the configured stream,
160 followed by a newline.
162 The following methods provide the implementations for the corresponding
163 functions of the same names. Using these methods on an instance is slightly
164 more efficient since new :class:`PrettyPrinter` objects don't need to be
168 .. method:: PrettyPrinter.isreadable(object)
170 .. index:: builtin: eval
172 Determine if the formatted representation of the object is "readable," or can be
173 used to reconstruct the value using :func:`eval`. Note that this returns
174 ``False`` for recursive objects. If the *depth* parameter of the
175 :class:`PrettyPrinter` is set and the object is deeper than allowed, this
179 .. method:: PrettyPrinter.isrecursive(object)
181 Determine if the object requires a recursive representation.
183 This method is provided as a hook to allow subclasses to modify the way objects
184 are converted to strings. The default implementation uses the internals of the
185 :func:`saferepr` implementation.
188 .. method:: PrettyPrinter.format(object, context, maxlevels, level)
190 Returns three values: the formatted version of *object* as a string, a flag
191 indicating whether the result is readable, and a flag indicating whether
192 recursion was detected. The first argument is the object to be presented. The
193 second is a dictionary which contains the :func:`id` of objects that are part of
194 the current presentation context (direct and indirect containers for *object*
195 that are affecting the presentation) as the keys; if an object needs to be
196 presented which is already represented in *context*, the third return value
197 should be ``True``. Recursive calls to the :meth:`format` method should add
198 additional entries for containers to this dictionary. The third argument,
199 *maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
200 is no requested limit. This argument should be passed unmodified to recursive
201 calls. The fourth argument, *level*, gives the current level; recursive calls
202 should be passed a value less than that of the current call.
204 .. versionadded:: 2.3
211 This example demonstrates several uses of the :func:`pprint` function and its parameters.
214 >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
215 ... ('parrot', ('fresh fruit',))))))))
216 >>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
217 >>> pprint.pprint(stuff)
222 ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
223 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
224 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
225 >>> pprint.pprint(stuff, depth=3)
227 ('spam', ('eggs', (...))),
228 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
229 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
230 >>> pprint.pprint(stuff, width=60)
236 ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
237 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
238 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
239 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]