On this page
pprint.PrettyPrinter
Prev: pprint.pprint | Up: pprint | Next: pprint.saferepr
Rel: pprint.isreadable, pprint.isrecursive, pprint.pformat, pprint.pp
Ref: docs.python.org — pprint.PrettyPrinter
Tags: #public #python3 #stdlib #pprint
Import
import pprint
from pprint import PrettyPrinter
Signature & Overview
pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True, underscore_numbers=False)
pprint.PrettyPrinter class.
Methods & Attributes
PrettyPrinter.format(self, object, context, maxlevels, level)— Format object for a specific context, returning a string and flags indicating whether the representation is 'readable' and whether the object represents a recursive construct.PrettyPrinter.isreadable(self, object)—PrettyPrinter.isreadablemethod/property.PrettyPrinter.isrecursive(self, object)—PrettyPrinter.isrecursivemethod/property.PrettyPrinter.pformat(self, object)—PrettyPrinter.pformatmethod/property.PrettyPrinter.pprint(self, object)—PrettyPrinter.pprintmethod/property.
Usage
import pprint
pp = pprint.PrettyPrinter(indent=2, width=40, sort_dicts=False)
out = pp.pformat({"b": [1, 2], "a": [3, 4]})
assert out.index("'b'") < out.index("'a'")
Gotchas
- Pass
sort_dicts=Falseto preserve insertion order: Defaultsort_dicts=Truesorts keys alphabetically.
REPL Inspect
from pprint import PrettyPrinter
help(PrettyPrinter)
Linked from
Related notes
- tuts/python3/stdlib_module/pprint/pprint Linked here; links to this page; shared tags: #pprint, #stdlib
- tuts/python3/stdlib_module/pprint/saferepr Linked here; links to this page; shared tags: #pprint, #stdlib
- tuts/python3/stdlib_module/pprint/isreadable Linked here; shared tags: #pprint, #stdlib
- tuts/python3/stdlib_module/pprint/isrecursive Linked here; shared tags: #pprint, #stdlib
- tuts/python3/stdlib_module/pprint/pformat Linked here; shared tags: #pprint, #stdlib
- tuts/python3/stdlib_module/pprint/pp Linked here; shared tags: #pprint, #stdlib
- tuts/python3/stdlib_module/pprint links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib