Skip to main content

Lib/pprint.py (part 2)

Source:

cpython 3.14 @ ab2d84fe1023/Lib/pprint.py

This annotation covers the per-type formatting methods. See lib_pprint_detail for pprint, pformat, PrettyPrinter.__init__, _format, and _repr.

Map

LinesSymbolRole
1-80_pprint_dictFormat a dict with sorted keys and indented values
81-180_pprint_ordered_dictFormat OrderedDict preserving insertion order
181-300_pprint_list / _pprint_tupleFormat list/tuple with per-element recursion
301-420_pprint_set / _pprint_frozensetFormat set/frozenset with sorted elements
421-560_pprint_dataclassFormat dataclass instances field by field
561-700_format_itemsShared helper for sequences and mappings

Reading

_pprint_dict

# CPython: Lib/pprint.py:280 _pprint_dict
def _pprint_dict(self, object, stream, indent, allowance, context, level):
write = stream.write
write('{')
if self._sort_dicts:
items = sorted(object.items(), key=_safe_key)
else:
items = object.items()
self._format_dict_items(items, stream, indent, allowance + 1,
context, level)
write('}')

When sort_dicts=True (the default), dict keys are sorted using a _safe_key wrapper that handles mixed-type keys without raising TypeError. Large dicts are split across lines with each key-value pair indented.

_format_items

# CPython: Lib/pprint.py:560 _format_items
def _format_items(self, items, stream, indent, allowance, context, level):
write = stream.write
indent += self._indent_per_level
delimnl = ',\n' + ' ' * indent
delim = ''
max_width1 = max_width = self._width - indent + 1
for item in items:
# Try to fit item on the current line
w = len(delim) + self._repr(item, context, level).width
if self._compact and w <= max_width:
write(delim)
self._repr(item, context, level).write(stream)
max_width -= w
else:
write(delim)
write('\n' + ' ' * indent)
self._format(item, stream, indent, allowance, context, level + 1)
max_width = max_width1
delim = ','

_format_items is the shared loop used by list, tuple, set, and frozenset formatters. compact=True packs items on the same line until the width limit is reached, then wraps.

_pprint_dataclass

# CPython: Lib/pprint.py:430 _pprint_dataclass
def _pprint_dataclass(self, object, stream, indent, allowance, context, level):
cls_name = object.__class__.__name__
indent += len(cls_name) + 1
items = [(f.name, getattr(object, f.name))
for f in dataclasses.fields(object)
if f.repr]
return self._format_namespace_items(items, stream, indent, allowance,
context, level)

Dataclass instances are formatted as ClassName(field=value, ...). Only fields with repr=True (the default) are included.

_pprint_set

# CPython: Lib/pprint.py:310 _pprint_set
def _pprint_set(self, object, stream, indent, allowance, context, level):
if not object:
stream.write(repr(object))
return
typ = object.__class__
if typ is set:
stream.write('{')
endchar = '}'
else:
stream.write(typ.__name__ + '({')
endchar = '})'
indent += len(typ.__name__) + 1
object = sorted(object, key=_safe_key)
self._format_items(object, stream, indent, allowance, context, level)
stream.write(endchar)

Sets are sorted for deterministic output. Empty sets print as set() since {} would be an empty dict.

gopy notes

pprint is pure Python. PrettyPrinter dispatch is via a _dispatch dict mapping type.__repr__ to handler functions. _pprint_dataclass calls dataclasses.fields from module/dataclasses. _safe_key is a Go struct in module/pprint/module.go with a custom Less that catches panics from comparison.