@@ -261,11 +261,29 @@ are converted to strings. The default implementation uses the internals of the
261261The "__pprint__" protocol
262262-------------------------
263263
264- Pretty printing will use an object's ``__repr__ `` by default. For custom pretty printing, objects can
264+ Pretty printing uses an object's ``__repr__ `` by default. For custom pretty printing, objects can
265265implement 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.
266+ exists, it is called instead of ``__repr__ ``. The method is called with a single argument, the object to be
267+ pretty printed.
268+
269+ The method is expected to return or yield a sequence of values, which are used to construct a pretty
270+ representation of the object. These values are wrapped in standard class "chrome", such as the class name.
271+ The printed representation will usually look like a class constructor, with positional, keyword, and default
272+ arguments. The values can be any of the following formats:
273+
274+ * A single value, representing a positional argument. The value itself is used.
275+ * A 2-tuple of ``(name, value) `` representing a keyword argument. A representation of
276+ ``name=value `` is used.
277+ * A 3-tuple of ``(name, value, default_value) `` representing a keyword argument with a default
278+ value. If ``value `` equals ``default_value ``, then this tuple is skipped, otherwise
279+ ``name=value `` is used.
280+
281+ .. note ::
282+
283+ This protocol is compatible with the `Rich library's pretty printing protocol
284+ <https://rich.readthedocs.io/en/latest/pretty.html#rich-repr-protocol> `_.
285+
286+ See the :ref: `pprint-protocol-example ` for how this can be used in practice.
269287
270288.. _pprint-example :
271289
@@ -432,11 +450,37 @@ cannot be split, the specified width will be exceeded::
432450 'summary': 'A sample Python project',
433451 'version': '1.2.0'}
434452
435- A custom ``__pprint__() `` method can be used to customize the representation of the object::
453+ .. _pprint-protocol-example :
454+
455+ Pretty Print Protocol Example
456+ -----------------------------
457+
458+ Let's start with a simple class that defines a ``__pprint__() `` method:
459+
460+ .. code-block :: python
461+
462+ class Bass :
463+ def __init__ (self , strings : int , pickups : str , active : bool = False ):
464+ self ._strings = strings
465+ self ._pickups = pickups
466+ self ._active = active
467+
468+ def __pprint__ (self ):
469+ yield self ._strings
470+ yield ' pickups' , self ._pickups
471+ yield ' active' , self ._active, False
472+
473+ precision = Bass(4 , ' split coil P' , active = False )
474+ stingray = Bass(5 , ' humbucker' , active = True )
475+
476+ The ``__pprint__() `` method yields three values, which correspond to the ``__init__() `` arguments,
477+ showing by example each of the three different allowed formats. Here is what the output looks like:
478+
479+ .. code-block :: pycon
480+
481+ >>> pprint.pprint(precision)
482+ Bass(4, pickups='split coil P')
483+ >>> pprint.pprint(stingray)
484+ Bass(5, pickups='humbucker', active=True)
436485
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
486+ Note that you'd get exactly the same output if you used ``print(..., pretty=True) ``.
0 commit comments