Python Glossary Русский

all Python 3pprintterm

pprint.PrettyPrinter

printing

A configurable pretty printer: width, indent, depth and key sorting are set once and applied by its pprint() and pformat() methods.

Syntax

pp = pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True, underscore_numbers=False)

Useful when the same settings are needed many times — a narrow output for a log, say. Positional arguments are ordered differently from pprint.pp(): here it is PrettyPrinter(indent, width, depth, stream), while pp takes stream second, so pass settings by name. sort_dicts defaults to True — keys come sorted, not in insertion order. depth replaces deeper levels with '...'.

Examples

import pprint
printer = pprint.PrettyPrinter(width=30, depth=2)
data = {'user': {'name': 'Ann', 'roles': ['admin', 'dev']}, 'id': 7}
print(printer.pformat(data))
# → {'id': 7, / 'user': {'name': 'Ann', / 'roles': [...]}}

See also

Open in the full glossarydocs.python.orgReport an inaccuracy