diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2013-10-02 11:56:18 +0300 |
|---|---|---|
| committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-10-02 11:56:18 +0300 |
| commit | 7c411a40413d9525aeb6114d71be3e012dbf81f5 (patch) | |
| tree | 240af349b039b0efd5c52903bff110748815280a /Doc/library/pprint.rst | |
| parent | 092bd388ced26650cf0a5a4838a87f7ca8a9ea97 (diff) | |
| download | cpython-git-7c411a40413d9525aeb6114d71be3e012dbf81f5.tar.gz | |
Issue #19132: The pprint module now supports compact mode.
Diffstat (limited to 'Doc/library/pprint.rst')
| -rw-r--r-- | Doc/library/pprint.rst | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/Doc/library/pprint.rst b/Doc/library/pprint.rst index 8d57c5d4ed..ec9de43cb2 100644 --- a/Doc/library/pprint.rst +++ b/Doc/library/pprint.rst @@ -29,14 +29,14 @@ The :mod:`pprint` module defines one class: .. First the implementation class: -.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None) +.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \ + compact=False) Construct a :class:`PrettyPrinter` instance. This constructor understands several keyword parameters. An output stream may be set using the *stream* keyword; the only method used on the stream object is the file protocol's :meth:`write` method. If not specified, the :class:`PrettyPrinter` adopts - ``sys.stdout``. Three additional parameters may be used to control the - formatted representation. The keywords are *indent*, *depth*, and *width*. The + ``sys.stdout``. The amount of indentation added for each recursive level is specified by *indent*; the default is one. Other values can cause output to look a little odd, but can make nesting easier to spot. The number of levels which may be printed is @@ -45,7 +45,9 @@ The :mod:`pprint` module defines one class: the depth of the objects being formatted. The desired output width is constrained using the *width* parameter; the default is 80 characters. If a structure cannot be formatted within the constrained width, a best effort will - be made. + be made. If *compact* is false (the default) each item of a long sequence + will be formatted on a separate line. If *compact* is true, as many items + as will fit within the *width* will be formatted on each output line. >>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] @@ -58,6 +60,12 @@ The :mod:`pprint` module defines one class: 'lumberjack', 'knights', 'ni'] + >>> pp = pprint.PrettyPrinter(width=41, compact=True) + >>> pp.pprint(stuff) + [['spam', 'eggs', 'lumberjack', + 'knights', 'ni'], + 'spam', 'eggs', 'lumberjack', 'knights', + 'ni'] >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ... ('parrot', ('fresh fruit',)))))))) >>> pp = pprint.PrettyPrinter(depth=6) @@ -67,21 +75,22 @@ The :mod:`pprint` module defines one class: The :mod:`pprint` module also provides several shortcut functions: -.. function:: pformat(object, indent=1, width=80, depth=None) +.. function:: pformat(object, indent=1, width=80, depth=None, *, compact=False) - Return the formatted representation of *object* as a string. *indent*, *width* - and *depth* will be passed to the :class:`PrettyPrinter` constructor as - formatting parameters. + Return the formatted representation of *object* as a string. *indent*, + *width*, *depth* and *compact* will be passed to the :class:`PrettyPrinter` + constructor as formatting parameters. -.. function:: pprint(object, stream=None, indent=1, width=80, depth=None) +.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \ + compact=False) Prints the formatted representation of *object* on *stream*, followed by a newline. If *stream* is ``None``, ``sys.stdout`` is used. This may be used in the interactive interpreter instead of the :func:`print` function for inspecting values (you can even reassign ``print = pprint.pprint`` for use - within a scope). *indent*, *width* and *depth* will be passed to the - :class:`PrettyPrinter` constructor as formatting parameters. + within a scope). *indent*, *width*, *depth* and *compact* will be passed + to the :class:`PrettyPrinter` constructor as formatting parameters. >>> import pprint >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] |
