Fundamentals 7 min read

Boost Your Python Development with the Powerful PrettyPrinter Library

PrettyPrinter is a Python 3.6+ library that delivers high‑quality, syntax‑highlighted pretty‑printing by improving on existing tools like __repr__, pprint, pprintpp, and IPython.lib.pretty, offering a simple descriptive API for custom formatting and stateful object representation.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Boost Your Python Development with the Powerful PrettyPrinter Library

PrettyPrinter is a powerful pretty‑printing package for Python 3.6 and above. It adopts an improved Wadler‑Leijen layout algorithm and draws inspiration from Haskell’s prettyprinter, JavaScript’s Prettier, Ruby’s prettypreinter.rb, and IPython’s pretty module, making it the most capable formatting tool in the Python ecosystem.

Why does Python still need an extra pretty‑printing package? Built‑in __repr__ and __str__ only return plain strings, the standard library pprint module handles only built‑in containers and uses a greedy layout algorithm that often fails, pprintpp is a limited improvement, and IPython’s IPython.lib.pretty offers better results but still requires knowledge of layout algorithms and can have side‑effects.

To overcome these limitations, the author implemented three key improvements:

An algorithm that maximizes pretty‑printing quality even at a modest performance cost, making a tenth‑second formatting overhead worthwhile.

A super‑simple, descriptive API that lets users register custom pretty‑printing rules without rewriting __repr__.

Robust syntax highlighting that never crashes on invalid Python syntax.

The new library delivers impressive performance and a straightforward user experience. Custom formatting is defined via two descriptive functions, register_pretty and pretty_call. For example: pretty_call('MyClass', arg1, arg2) The internal layout process follows a series of statements illustrated in the accompanying diagrams.

Defining a custom type is easy. Using register_pretty as a decorator, you can specify how MyClass should be displayed:

@register_pretty(MyClass)
def _pretty_myclass(obj, ctx):
    return pretty_call('MyClass', obj.attr1, obj.attr2)

The output produced by cpprint is clean and readable, as shown in the screenshots.

For stateful instances, PrettyPrinter supports explanatory comments that appear in the output, enabling clear representation of objects like a Connection class with "connected" and "disconnected" states.

In conclusion, PrettyPrinter greatly enhances the developer experience, especially within IPython where all results are automatically formatted. The project’s source code and documentation are available on GitHub and Read the Docs, with built‑in support for Django models, QuerySets, and attrs‑based classes.

English original: https://tommikaikkonen.github.io/introducing-prettyprinter-for-python/ – Translator: woody
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingPythonCode FormattingIPythonpretty printing
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.