-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
PEP 813: The Pretty Print Protocol #4690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
398f467
25c70f7
5555617
cd0a257
1d4b9d1
b8fc7e4
263a95b
143cb6c
a8598f7
1a96c8d
2703f5c
686fc4d
94d20f5
1cf369c
062eef6
8f744fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,196 @@ | ||||||||||||||
| PEP: 813 | ||||||||||||||
| Title: The Pretty Print Protocol | ||||||||||||||
| Author: Barry Warsaw <[email protected]> | ||||||||||||||
| Discussions-To: Pending | ||||||||||||||
| Status: Draft | ||||||||||||||
| Type: Standards Track | ||||||||||||||
| Created: 07-Nov-2025 | ||||||||||||||
| Python-Version: 3.15 | ||||||||||||||
| Post-History: Pending | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| Abstract | ||||||||||||||
| ======== | ||||||||||||||
|
|
||||||||||||||
| This PEP describes the "pretty print protocol", a collection of changes proposed to make pretty printing more | ||||||||||||||
| customizable and convenient. | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| Motivation | ||||||||||||||
| ========== | ||||||||||||||
|
|
||||||||||||||
| "Pretty printing" is a feature which provides a capability to format object representations for better | ||||||||||||||
| readability. The core functionality is implemented by the standard library :mod:`pprint`. ``pprint`` | ||||||||||||||
warsaw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| includes a class and APIs which users can invoke to format and print more readable representations of objects. | ||||||||||||||
warsaw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
| Important use cases include pretty printing large dictionaries and other complicated objects. | ||||||||||||||
warsaw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| The ``pprint`` module is great as far as it goes. This PEP builds on the features of this module to provide | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| more customization and convenience. | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| Rationale | ||||||||||||||
| ========= | ||||||||||||||
|
|
||||||||||||||
| Pretty printing is very useful for displaying complex data structures, like dictionaries read from JSON | ||||||||||||||
| 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. | ||||||||||||||
|
Comment on lines
+35
to
+37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
|
Comment on lines
+39
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| These two extensions work independently, but hand-in-hand can provide a powerful and convenient new feature. | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| Specification | ||||||||||||||
| ============= | ||||||||||||||
|
|
||||||||||||||
| There are two parts to this proposal. | ||||||||||||||
warsaw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| ``__pretty__()`` methods | ||||||||||||||
| ------------------------ | ||||||||||||||
|
|
||||||||||||||
| Classes can implement a new dunder method, ``__pretty__()`` which if present, generates the pretty printed | ||||||||||||||
| representation of their instances. This augments ``__repr__()`` which, prior to this proposal, was the only | ||||||||||||||
| 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. | ||||||||||||||
|
||||||||||||||
| distinct from pretty printing, some classes may want more control over their pretty display. | |
| structured representation distinct from pretty printing, some classes may benefit from more control over pretty visual display of their instances. |
There was a problem hiding this comment.
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?
warsaw marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
naming: suggest using an underscore?
| * ``maxlevels`` - the requested limit to recursion | |
| * ``maxlevels`` - the requested limit to recursion (integer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is the current level, should it be singular instead of plural?
| * ``levels`` - the current recursion level | |
| * ``levels`` - the current recursion level (integer) |
warsaw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| :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 |
Uh oh!
There was an error while loading. Please reload this page.