On this page
reprlib
Prev: readline | Up: stdlib_module | Next: resource
Rel: datetime, zoneinfo, calendar, collections, heapq
Ref: docs.python.org — reprlib
Tags: #public #python3 #stdlib
Import
import reprlib
Overview
Size-bounded repr() generator (Repr, reprlib.repr(), @recursive_repr) that truncates long strings, deep nested containers, and circular references into compact summaries. Use reprlib in logging, debuggers, and custom __repr__ methods so massive data structures don't flood logs or trigger RecursionError.
Quick Start
import reprlib
big = list(range(1000))
assert reprlib.repr(big) == "[0, 1, 2, 3, 4, 5, ...]"
Implementation Walkthrough
1. Customizing Limits with a Repr Instance
Instantiate reprlib.Repr to tune maxlevel, maxlist, maxdict, maxstring, or enable pretty indentation (indent=2):
import reprlib
short = reprlib.Repr(maxlist=3, maxstring=12)
assert short.repr([1, 2, 3, 4, 5]) == "[1, 2, 3, ...]"
assert short.repr("abcdefghijklmnopqrstuvwxyz") == "'abc...xyz'"
2. Guarding Custom Containers Against Circular __repr__
Decorate __repr__ with @reprlib.recursive_repr() so self-referential objects print ... instead of crashing with RecursionError:
import reprlib
class Node:
def __init__(self, name: str) -> None:
self.name = name
self.children: list[Node] = []
@reprlib.recursive_repr()
def __repr__(self) -> str:
return f"Node({self.name!r}, {self.children!r})"
root = Node("root")
root.children.append(root)
assert repr(root) == "Node('root', [...])"
Gotchas & Boundaries
- Truncated output is not valid Python syntax: Because
reprlibinserts..., its output is for human diagnostics and logging—never pass it toast.literal_eval(). - Keep
aRepruntouched: Rather than mutating the module-globalreprlib.aRepr, create your ownreprlib.Repr(...)instance so you don't affect other callers. - Pair with pprint for full inspection: Use
reprlibwhen you want bounded one-line summaries; use pprint when you want complete, readable multi-line dumps.
Common Functions
- reprlib.recursive_repr() —
recursive_repr(fillvalue='...')— Decorator to make a repr function return fillvalue for a recursive call - reprlib.repr() —
repr(x)—reprlib.repr()function.
Classes & Objects
- reprlib.Repr —
reprlib.Reprclass. (Methods:.repr(),.repr1(),.repr_array(),.repr_deque(),.repr_dict(),.repr_frozenset())
Linked from
Related notes
- tuts/python3/stdlib_module/readline Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/reprlib/recursive_repr Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/reprlib/repr Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/reprlib/Repr_class Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/resource Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/calendar Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/collections Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/datetime Linked here; shared tags: #stdlib