AI Skill Report Card

Using Reprlib

A-81·Aug 22, 2026·Source: Web
14 / 15
Python
import reprlib # Truncated repr for large data r = reprlib.Repr() r.maxlist = 3 print(r.repr(list(range(100)))) # [0, 1, 2, ...] # Quick one-off truncated repr print(reprlib.repr(list(range(100)))) # [0, 1, 2, ...] # Protect custom repr methods from infinite recursion class Tree: def __init__(self, children=None): self.children = children or [] @reprlib.recursive_repr() def __repr__(self): return f"Tree({self.children!r})" t = Tree() t.children = [t] # self-referential print(repr(t)) # Tree([...])
Recommendation
Add an example showing a bad outcome, e.g., forgetting recursive_repr and hitting RecursionError, to illustrate contrast
13 / 15

Progress:

  • Identify whether the need is (a) truncated repr for built-in-like containers/strings, or (b) recursion-safety for a custom __repr__
  • For (a): use reprlib.repr() for a quick default, or subclass/configure reprlib.Repr for custom limits
  • For (b): decorate __repr__ with @reprlib.recursive_repr()
  • Verify output length/behavior matches expectations
  • Integrate into logging/debugging code where output size matters

Using reprlib.Repr for custom limits

  1. Instantiate r = reprlib.Repr() (or subclass it for reusable config).
  2. Set relevant max* attributes before calling .repr():
    • maxlevel — max recursion depth (default 6)
    • maxtuple, maxlist, maxarray, maxdict, maxset, maxfrozenset, maxdeque — max elements shown per container type
    • maxstring — max characters shown for str
    • maxlong — max digits shown for int
    • maxother — max chars for other objects' repr
  3. Call r.repr(obj) for the general dispatcher, or type-specific methods like r.repr_list(obj, level) directly.
  4. To customize behavior for a specific type, subclass Repr and override repr_<typename>(self, obj, level).

Using recursive_repr decorator

  1. Import reprlib.recursive_repr.
  2. Apply as a decorator directly above __repr__ (must decorate __repr__ specifically, or any method returning a string representation).
  3. Optionally pass a fill-in string: @reprlib.recursive_repr(fillvalue='...') (default is '...').
  4. When a recursive call to the same object's __repr__ is detected (via a thread-local set of ids), the decorator returns the fill-in value instead of recursing infinitely.
Recommendation
Include guidance on performance/overhead considerations when using Repr in hot logging paths
17 / 20

Example 1: Truncated list repr Input:

Python
reprlib.repr(list(range(1000)))

Output:

Python
'[0, 1, 2, 3, 4, 5, 6, ...]'

Example 2: Custom Repr subclass Input:

Python
class MyRepr(reprlib.Repr): def __init__(self): super().__init__() self.maxstring = 10 self.maxlist = 2 aRepr = MyRepr() aRepr.repr(["hello world this is long", "b", "c", "d"])

Output:

Python
"['hello wor...', 'b', ...]"

Example 3: Recursive structure protection Input:

Python
class Node: def __init__(self): self.next = None @reprlib.recursive_repr() def __repr__(self): return f"Node(next={self.next!r})" n = Node() n.next = n repr(n)

Output:

Python
'Node(next=...)'

Example 4: Nested container truncation with maxlevel Input:

Python
r = reprlib.Repr() r.maxlevel = 2 r.repr([1, [2, [3, [4, [5]]]]])

Output:

Python
'[1, [2, [...]]]'
Recommendation
Mention interaction with dataclasses or attrs which auto-generate __repr__, since decorating them requires care
  • Use the module-level reprlib.repr() for quick, one-off truncation with sensible defaults — no need to instantiate Repr for simple cases.
  • Instantiate and configure a Repr object (or subclass) once and reuse it when truncation settings need to be consistent across many calls (e.g., in a logging utility).
  • Always apply @reprlib.recursive_repr() to __repr__ methods of classes that can form cycles or self-references (trees, graphs, linked structures, or containers that may hold themselves).
  • Prefer subclassing Repr and overriding repr_<type> methods when default truncation for a specific type isn't sufficient, rather than writing truncation logic manually.
  • Remember reprlib.repr() is meant for display/debug purposes — the output is not necessarily valid Python that can be eval()'d back into the original object once truncated.
  • Don't confuse reprlib.Repr.maxlevel behavior — it counts down levels, and nested containers beyond the limit collapse to ..., not to an empty container.
  • Don't apply @reprlib.recursive_repr() to methods other than the string-producing repr method — it's specifically designed to guard against recursive string conversion, using a thread-local + id-based guard.
  • Don't expect reprlib.repr() truncated output to be safe for eval(); it's for human-readable debugging only.
  • Don't forget that maxstring and maxlong apply per-value, not per-container — a container of many short strings can still produce a long overall output if maxlist/maxdict etc. are large.
  • Don't manually implement recursion guards with global mutable state when reprlib.recursive_repr() already handles thread-safety via thread-local storage.
0
Grade A-AI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
14/15
Workflow
13/15
Examples
17/20
Completeness
17/20
Format
14/15
Conciseness
14/15