reprlib Truncates Reprs Of Large Structures
reprlib caps entries per container and nesting depth when stringifying large structures, so logs and reports stay readable without hand-rolled truncation.
Code
import reprlib
huge = list(range(1000))
deep = {"level1": {"level2": {"level3": ["leaf"]}}}
# plain repr expands every element, so logging it floods the output
print("plain repr length:", len(repr(huge)))
# reprlib caps each kind of value: entries per container and nesting depth
r = reprlib.Repr()
r.maxlist = 5
r.maxdict = 3
r.maxlevel = 2
r.maxstring = 20
print(r.repr(huge))
print(r.repr(deep))How it works
Module-level bindings hold the data — huge, deep — with no function scope in between.
Watch out for
- The dependency list is empty; the language runtime covers it all.
Putting it to use
Output lines are prefixed (plain repr length:), making each step’s effect easy to spot.
Core lines
deep = {"level1": {"level2": {"level3": ["leaf"]}}}
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.HAR Waterfall AnalyzerInspect HTTP Archive (HAR 1.2) JSON files locally to summarize total requests, status codes, MIME types, slowest calls, and timing waterfalls.
FAQ
- Will the snippet run without installing packages?
- Yes, it runs as-is — nothing outside the built-in APIs is referenced. Every import resolves inside the standard library — `reprlib`. Its state lives in module-level bindings: `huge`, `deep`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.