Fundamentals 8 min read

Boost Python Readability with the Powerful PrettyPrinter Library

This article introduces Python's PrettyPrinter package, explains why native printing tools fall short, and demonstrates its advanced layout algorithm, syntax‑highlighting, and simple descriptive API for custom pretty‑printing, complete with usage examples and visual screenshots.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Boost Python Readability with the Powerful PrettyPrinter Library

PrettyPrinter is a powerful Python 3.6+ package for descriptive, syntax‑highlighted pretty‑printing, built on an improved Wadler‑Leijen layout algorithm and inspired by Haskell's prettyprinter, JavaScript's Prettier, Ruby's prettypreinter.rb, and IPython's pretty module.

Why does Python still need an extra pretty‑printing package? While IDEs and manual prints are the basic interface for developer‑data interaction, built‑in tools have limitations:

__repr__ and __str__ return plain strings; __repr__ aims for valid Python expressions but offers no formatting.

The standard pprint module formats built‑in containers but uses a greedy layout algorithm that often produces sub‑optimal output and cannot handle custom types beyond __repr__.

The third‑party pprintpp improves on pprint but still inherits its __repr__‑based constraints.

IPython’s IPython.lib.pretty provides a more advanced alternative with better algorithms and custom‑type support, yet it pushes data directly to a layout buffer and can have side effects.

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

An algorithm that maximizes pretty‑printing quality, even at a modest performance cost—spending a tenth of a second to format output saves seconds when searching results.

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

Robust syntax highlighting that gracefully handles invalid Python syntax without breaking.

The new package delivers impressive performance and an easy way to define custom pretty‑printing rules using two descriptive functions: register_pretty and pretty_call. Syntax highlighting looks polished and never aborts on malformed code, dramatically improving developer experience.

Simple, Descriptive API

PrettyPrinter defines output formatting through function calls. All non‑string Python values are represented by function results. The core function pretty_call describes the type of call to render. An example invocation is shown below:

The layout processing resembles the following diagram:

Using pretty_call, one can then define custom types. For example, registering a pretty printer for a MyClass:

Applying the register_pretty decorator creates a formatted output for MyClass instances:

The resulting output looks like:

Stateful Instance Representation

Function‑call based formatting struggles with stateful objects. PrettyPrinter solves this by inserting explanatory comments into the layout, which appear in the output. For a Connection class with "connected" and "disconnected" states, the following definitions produce annotated output:

In conclusion, the author highly recommends integrating PrettyPrinter into development workflows, especially within IPython, where all interactive results automatically benefit from the enhanced formatting. The package also includes ready‑made definitions for Django models, QuerySets, and attrs‑based classes, making it a valuable tool for Python developers.

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.

DebuggingCode Formattingpretty printingprettyprinter
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.