Skip to content

Commit bb23638

Browse files
committed
Flesh out the pprint protocol documentation
* Update the docs on the `print()` function to describe the new `pretty` keyword * Document the `__pprint__()` protocol. * Add a test for the `__pprint__()` protocol method.
1 parent 4d237da commit bb23638

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

Doc/library/functions.rst

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,17 +1587,23 @@ are always available. They are listed here in alphabetical order.
15871587
supported.
15881588

15891589

1590-
.. function:: print(*objects, sep=' ', end='\n', file=None, flush=False)
1590+
.. function:: print(*objects, sep=' ', end='\n', file=None, flush=False, pretty=None)
15911591

1592-
Print *objects* to the text stream *file*, separated by *sep* and followed
1593-
by *end*. *sep*, *end*, *file*, and *flush*, if present, must be given as keyword
1594-
arguments.
1592+
Print *objects* to the text stream *file*, separated by *sep* and followed by
1593+
*end*. *sep*, *end*, *file*, *flush*, and *pretty*, if present, must be
1594+
given as keyword arguments.
1595+
1596+
When *pretty* is ``None``, all non-keyword arguments are converted to
1597+
strings like :func:`str` does and written to the stream, separated by *sep*
1598+
and followed by *end*. Both *sep* and *end* must be strings; they can also
1599+
be ``None``, which means to use the default values. If no *objects* are
1600+
given, :func:`print` will just write *end*.
15951601

1596-
All non-keyword arguments are converted to strings like :func:`str` does and
1597-
written to the stream, separated by *sep* and followed by *end*. Both *sep*
1598-
and *end* must be strings; they can also be ``None``, which means to use the
1599-
default values. If no *objects* are given, :func:`print` will just write
1600-
*end*.
1602+
When *pretty* is given, it signals that the objects should be "pretty
1603+
printed". *pretty* can be ``True`` or an object implementing the
1604+
:method:`PrettyPrinter.pprint()` API which takes an object and returns a
1605+
formatted representation of the object. When *pretty* is ``True``, then it
1606+
actually does call ``PrettyPrinter.pformat()`` explicitly.
16011607

16021608
The *file* argument must be an object with a ``write(string)`` method; if it
16031609
is not present or ``None``, :data:`sys.stdout` will be used. Since printed
@@ -1611,6 +1617,9 @@ are always available. They are listed here in alphabetical order.
16111617
.. versionchanged:: 3.3
16121618
Added the *flush* keyword argument.
16131619

1620+
.. versionchanged:: 3.15
1621+
Added the *pretty* keyword argument.
1622+
16141623

16151624
.. class:: property(fget=None, fset=None, fdel=None, doc=None)
16161625

Doc/library/pprint.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ adjustable by the *width* parameter defaulting to 80 characters.
2828
.. versionchanged:: 3.10
2929
Added support for pretty-printing :class:`dataclasses.dataclass`.
3030

31+
.. versionchanged:: 3.15
32+
Added support for the :ref:`__pprint__ <dunder-pprint>` protocol.
33+
3134
.. _pprint-functions:
3235

3336
Functions
@@ -253,6 +256,16 @@ are converted to strings. The default implementation uses the internals of the
253256
calls. The fourth argument, *level*, gives the current level; recursive calls
254257
should be passed a value less than that of the current call.
255258

259+
.. _dunder-pprint:
260+
261+
The "__pprint__" protocol
262+
-------------------------
263+
264+
Pretty printing will use an object's ``__repr__`` by default. For custom pretty printing, objects can
265+
implement a ``__pprint__()`` function to customize how their representations will be printed. If this method
266+
exists, it is called with 4 arguments, exactly matching the API of :meth:`PrettyPrinter.format()`. The
267+
``__pprint__()`` method is expected to return a string, which is used as the pretty printed representation of
268+
the object.
256269

257270
.. _pprint-example:
258271

@@ -418,3 +431,12 @@ cannot be split, the specified width will be exceeded::
418431
'requires_python': None,
419432
'summary': 'A sample Python project',
420433
'version': '1.2.0'}
434+
435+
A custom ``__pprint__()`` method can be used to customize the representation of the object::
436+
437+
>>> class Custom:
438+
... def __str__(self): return 'my str'
439+
... def __repr__(self): return 'my repr'
440+
... def __pprint__(self, context, maxlevels, level): return 'my pprint'
441+
>>> pprint.pp(Custom())
442+
my pprint

Lib/test/test_pprint.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ def __ne__(self, other):
134134
def __hash__(self):
135135
return self._hash
136136

137+
138+
class CustomPrintable:
139+
def __str__(self):
140+
return "my str"
141+
142+
def __repr__(self):
143+
return "my str"
144+
145+
def __pprint__(self, context, maxlevels, level):
146+
return "my pprint"
147+
148+
137149
class QueryTestCase(unittest.TestCase):
138150

139151
def setUp(self):
@@ -1472,6 +1484,13 @@ def test_user_string(self):
14721484
'jumped over a '
14731485
'lazy dog'}""")
14741486

1487+
def test_custom_pprinter(self):
1488+
stream = io.StringIO()
1489+
pp = pprint.PrettyPrinter(stream=stream)
1490+
custom_obj = CustomPrintable()
1491+
pp.pprint(custom_obj)
1492+
self.assertEqual(stream.getvalue(), "my pprint\n")
1493+
14751494

14761495
class DottedPrettyPrinter(pprint.PrettyPrinter):
14771496

0 commit comments

Comments
 (0)