11PEP: 813
22Title: The Pretty Print Protocol
3- Author: Barry Warsaw <barry@python.org>
3+ Author: Barry Warsaw <barry@python.org>, Eric V. Smith <eric@trueblade.com>
44Discussions-To: Pending
55Status: Draft
66Type: Standards Track
@@ -21,8 +21,9 @@ Motivation
2121
2222"Pretty printing" is a feature which provides a capability to format object representations for better
2323readability. The core functionality is implemented by the standard library :mod: `pprint ` module. ``pprint ``
24- includes a class and APIs which users can invoke to format and print more readable representations of objects.
25- Important use cases include pretty printing large dictionaries and other complicated objects.
24+ includes a class and APIs which users can invoke to format and print more readable representations of objects,
25+ versus the standard ``repr() `` built-in function. Important use cases include pretty printing large
26+ dictionaries and other complicated objects for debugging purposes.
2627
2728The ``pprint `` module is great as far as it goes. This PEP builds on the features of this module to provide
2829more customization and convenience.
@@ -45,7 +46,7 @@ These two extensions work independently, but hand-in-hand can provide a powerful
4546Specification
4647=============
4748
48- There are two parts to this proposal.
49+ There are several parts to this proposal.
4950
5051
5152``__pprint__() `` methods
@@ -54,24 +55,28 @@ There are two parts to this proposal.
5455Classes can implement a new dunder method, ``__pprint__() `` which if present, generates the pretty printed
5556representation of their instances. This augments ``__repr__() `` which, prior to this proposal, was the only
5657method used to generate a pretty representation of the object. Since object reprs provide functionality
57- distinct from pretty printing, some classes may want more control over their pretty display.
58+ distinct from pretty printing, some classes may want more control over their pretty display. The
59+ :py:class: `python:pprint.PrettyPrinter ` class is modified to respect an object's ``__pprint__() `` method if
60+ present.
5861
5962``__pprint__() `` is optional; if missing, the standard pretty printers fall back to ``__repr__() ``
6063for full backward compatibility (technically speaking, :py:func: `python:pprint.saferepr ` is used).
6164However, if defined on a class, ``__pprint__() `` has the same argument signature as
6265:py:meth: `python:pprint.PrettyPrinter.format `, taking four arguments:
6366
64- * ``object `` - the object to print, which is effectively always `` self ``
67+ * ``self `` - this object (described in `` PrettyPrinter.format() `` method as `` object ``)
6568* ``context `` - a dictionary mapping the ``id() `` of objects which are part of the current presentation
6669 context
6770* ``maxlevels `` - the requested limit to recursion
6871* ``levels `` - the current recursion level
6972
70- Similarly, ``__pprint__() `` returns three values, the string to be used as the pretty printed representation,
71- a boolean indicating whether the returned value is "readable", and a boolean indicating whether recursion has
72- been detected. In this context, "readable" means the same as
73- :py:meth: `python:pprint.PrettyPrinter.isreadable `, i.e. that the returned value can be used to reconstruct the
74- original object using ``eval() ``.
73+ Similarly, ``__pprint__() `` returns three values:
74+
75+ * the string to be used as the pretty printed representation
76+ * a boolean indicating whether the returned value is "readable" (defined by
77+ :py:meth: `python:pprint.PrettyPrinter.isreadable `, i.e. that the returned value can be used to reconstruct the
78+ original object using ``eval() ``).
79+ * boolean indicating whether recursion has been detected.
7580
7681See :py:meth: `python:pprint.PrettyPrinter.format ` for details.
7782
0 commit comments