Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix markup errors
  • Loading branch information
warsaw committed Nov 7, 2025
commit 55556175c45d04fc8b36784159e61278c55f6315
31 changes: 16 additions & 15 deletions peps/pep-0813.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PEP: 813
Title: The Pretty Print Protocol
Author: Barry Warsaw <[email protected]>,
Eric V. Smith <eric at trueblade.com>
Eric V. Smith <eric@trueblade.com>
Discussions-To: Pending
Status: Draft
Type: Standards Track
Expand Down Expand Up @@ -36,7 +36,7 @@ Pretty printing is very useful for displaying complex data structures, like dict
content. By providing a way for classes to customize how their instances participate in pretty printing,
users have more options for visually improving the display and debugging of their complex data.

By extending the built-in :py:func:`print` function to automatically pretty print its output, this feature is
By extending the built-in :func:`print` function to automatically pretty print its output, this feature is
made even more convenient, since no extra imports are required, and users can easily just piggyback on
well-worn "print debugging" patterns, at least for the most common use cases.

Expand All @@ -57,10 +57,10 @@ representation of their instances. This augments ``__repr__()`` which, prior to
method used to generate a pretty representation of the object. Since object reprs provide functionality
distinct from pretty printing, some classes may want more control over their pretty display.

``__pretty__()`` is optional; if missing, the standard pretty printers fall back to ``__repr__()`` for full
backward compatibility (technically speaking, :meth:`pprint.saferepr` is used). However, if defined on a
class, ``__pretty__()`` has the same argument signature as :py:func:`PrettyPrinter.format`, taking four
arguments:
``__pretty__()`` is optional; if missing, the standard pretty printers fall back to ``__repr__()``
for full backward compatibility (technically speaking, :py:func:`python:pprint.saferepr` is used).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is always using saferepr part of the PEP spec here?

However, if defined on a class, ``__pretty__()`` has the same argument signature as
:py:meth:`python:pprint.PrettyPrinter.format`, taking four arguments:

* ``object`` - the object to print, which is effectively always ``self``
* ``context`` - a dictionary mapping the ``id()`` of objects which are part of the current presentation
Expand All @@ -70,23 +70,24 @@ arguments:

Similarly, ``__pretty__()`` returns three values, the string to be used as the pretty printed representation,
a boolean indicating whether the returned value is "readable", and a boolean indicating whether recursion has
been detected. In this context, "readable" means the same as :meth:`PrettyPrinter.isreadable`, i.e. that the
returned value can be used to reconstruct the original object using ``eval()``.
been detected. In this context, "readable" means the same as
:py:meth:`python:pprint.PrettyPrinter.isreadable`, i.e. that the returned value can be used to reconstruct the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:py:meth:`python:pprint.PrettyPrinter.isreadable`, i.e. that the returned value can be used to reconstruct the
:py:meth:`~pprint.PrettyPrinter.isreadable`, i.e. that the returned value can be used to reconstruct the

original object using ``eval()``.

See :py:func:`PrettyPrinter.format` for details.
See :py:meth:`python:pprint.PrettyPrinter.format` for details.


A new argument to built-in ``print``
------------------------------------

Built-in :py:func:`print` takes a new optional argument, appended to the end of the argument list, called
Built-in :func:`print` takes a new optional argument, appended to the end of the argument list, called
``pretty``, which can take one of the following values:

* ``None`` - the default; fully backward compatible
* ``True`` - use a temporary instance of the :py:class:`PrettyPrinter` class to get a pretty representation of
the object.
* ``True`` - use a temporary instance of the :py:class:`python:pprint.PrettyPrinter` class to get a
pretty representation of the object.
* An instance with a ``pformat()`` method, which has the same signature as
:meth:`PrettyPrinter.pformat`. When given, this will usually be an instance of a subclass of
:py:meth:`python:pprint.PrettyPrinter.pformat`. When given, this will usually be an instance of a subclass of
`PrettyPrinter` with its `pformat()` method overridden. Note that this form requires **an
instance** of a pretty printer, not a class, as only ``print(..., pretty=True)`` performs implicit
instantiation.
Expand All @@ -97,7 +98,7 @@ Examples

A custom ``__pprint__()`` method can be used to customize the representation of the object:

.. _code-block:
.. code-block::

>>> class Custom:
... def __str__(self): return 'my str'
Expand All @@ -109,7 +110,7 @@ A custom ``__pprint__()`` method can be used to customize the representation of

Using the ``pretty`` argument to ``print()``:

.. _code-block:
.. code-block::

>>> import os
>>> print(os.pathconf_names)
Expand Down
Loading