Skip to main content

Lib/pprint.py

Source:

cpython 3.14 @ ab2d84fe1023/Lib/pprint.py

Lib/pprint.py provides PrettyPrinter and the pprint, pformat, isreadable, isrecursive module-level functions. PrettyPrinter formats Python objects with indentation and line-width control, detecting cycles and abbreviating repeated objects.

Map

LinesSymbolRole
1-50imports, _id_setsCycle detection set; constants
51-200PrettyPrinter.__init__, pprint, pformatPrinter configuration; entry points
201-400_pprint_dict, _pprint_list, _pprint_tuple, _pprint_setType-specific formatters
401-500_format, _reprRecursive dispatch; width-aware line splitting
501-600isreadable, isrecursive, _safe_reprUtility functions

Reading

Width-aware line splitting

_format(object, stream, indent, allowance, context, level) first computes repr(object). If it fits within the remaining width (self._width - indent - allowance), it is written as a single line. Otherwise the type-specific formatter is called to write an indented multi-line form.

# CPython: Lib/pprint.py:401 _format
def _format(self, object, stream, indent, allowance, context, level):
rep = self._repr(object, context, level)
max_width = self._width - indent - allowance
if len(rep) <= max_width:
stream.write(rep)
return
p = self._dispatch.get(type(object).__repr__, None)
if p is not None:
p(self, object, stream, indent, allowance, context, level + 1)
else:
stream.write(rep)

Cycle detection

context is a set of id() values of objects currently being formatted. Before recursing into a container, _format checks whether id(object) in context. If so it writes ... instead of recursing, preventing infinite loops.

# CPython: Lib/pprint.py:420 cycle check
if id(object) in context:
stream.write('...')
return
context.add(id(object))
...
context.discard(id(object))

Dispatch table

_dispatch maps type.__repr__ methods to type-specific printer functions. This allows PrettyPrinter to be extended for custom types by subclassing and adding entries to _dispatch.

gopy notes

Not yet ported. The planned package path is module/pprint/. A Go implementation would use fmt.Stringer-style dispatch and a strings.Builder for the output buffer.