11PEP: 813
22Title: The Pretty Print Protocol
33Author: Barry Warsaw <barry@python.org>,
4- Eric V. Smith <eric at trueblade.com>
4+ Eric V. Smith <eric@ trueblade.com>
55Discussions-To: Pending
66Status: Draft
77Type: Standards Track
@@ -36,7 +36,7 @@ Pretty printing is very useful for displaying complex data structures, like dict
3636content. By providing a way for classes to customize how their instances participate in pretty printing,
3737users have more options for visually improving the display and debugging of their complex data.
3838
39- By extending the built-in :py: func: `print ` function to automatically pretty print its output, this feature is
39+ By extending the built-in :func: `print ` function to automatically pretty print its output, this feature is
4040made even more convenient, since no extra imports are required, and users can easily just piggyback on
4141well-worn "print debugging" patterns, at least for the most common use cases.
4242
@@ -57,10 +57,10 @@ representation of their instances. This augments ``__repr__()`` which, prior to
5757method used to generate a pretty representation of the object. Since object reprs provide functionality
5858distinct from pretty printing, some classes may want more control over their pretty display.
5959
60- ``__pretty__() `` is optional; if missing, the standard pretty printers fall back to ``__repr__() `` for full
61- backward compatibility (technically speaking, :meth: ` pprint.saferepr ` is used). However, if defined on a
62- class, ``__pretty__() `` has the same argument signature as :py:func: ` PrettyPrinter.format `, taking four
63- arguments:
60+ ``__pretty__() `` is optional; if missing, the standard pretty printers fall back to ``__repr__() ``
61+ for full backward compatibility (technically speaking, :py:func: ` python: pprint.saferepr ` is used).
62+ However, if defined on a class, ``__pretty__() `` has the same argument signature as
63+ :py:meth: ` python:pprint.PrettyPrinter.format `, taking four arguments:
6464
6565* ``object `` - the object to print, which is effectively always ``self ``
6666* ``context `` - a dictionary mapping the ``id() `` of objects which are part of the current presentation
@@ -70,23 +70,24 @@ arguments:
7070
7171Similarly, ``__pretty__() `` returns three values, the string to be used as the pretty printed representation,
7272a boolean indicating whether the returned value is "readable", and a boolean indicating whether recursion has
73- been detected. In this context, "readable" means the same as :meth: `PrettyPrinter.isreadable `, i.e. that the
74- returned value can be used to reconstruct the original object using ``eval() ``.
73+ been detected. In this context, "readable" means the same as
74+ :py:meth: `python:pprint.PrettyPrinter.isreadable `, i.e. that the returned value can be used to reconstruct the
75+ original object using ``eval() ``.
7576
76- See :py:func: ` PrettyPrinter.format ` for details.
77+ See :py:meth: ` python:pprint. PrettyPrinter.format ` for details.
7778
7879
7980A new argument to built-in ``print ``
8081------------------------------------
8182
82- Built-in :py: func: `print ` takes a new optional argument, appended to the end of the argument list, called
83+ Built-in :func: `print ` takes a new optional argument, appended to the end of the argument list, called
8384``pretty ``, which can take one of the following values:
8485
8586* ``None `` - the default; fully backward compatible
86- * ``True `` - use a temporary instance of the :py:class: `PrettyPrinter ` class to get a pretty representation of
87- the object.
87+ * ``True `` - use a temporary instance of the :py:class: `python:pprint. PrettyPrinter ` class to get a
88+ pretty representation of the object.
8889* An instance with a ``pformat() `` method, which has the same signature as
89- :meth: `PrettyPrinter.pformat `. When given, this will usually be an instance of a subclass of
90+ :py: meth: `python:pprint. PrettyPrinter.pformat `. When given, this will usually be an instance of a subclass of
9091 `PrettyPrinter ` with its `pformat() ` method overridden. Note that this form requires **an
9192 instance ** of a pretty printer, not a class, as only ``print(..., pretty=True) `` performs implicit
9293 instantiation.
@@ -97,7 +98,7 @@ Examples
9798
9899A custom ``__pprint__() `` method can be used to customize the representation of the object:
99100
100- .. _ code -block :
101+ .. code -block: :
101102
102103 >>> class Custom:
103104 ... def __str__(self): return 'my str'
@@ -109,7 +110,7 @@ A custom ``__pprint__()`` method can be used to customize the representation of
109110
110111 Using the ``pretty `` argument to ``print() ``:
111112
112- .. _ code -block :
113+ .. code -block: :
113114
114115 >>> import os
115116 >>> print(os.pathconf_names)
0 commit comments