Python Glossary Русский

Python 3.0+reprlibterm

reprlib.Repr

abbreviated repr

A configurable abbreviated repr: it limits the number of items, string length and nesting depth through attributes such as maxlist, maxstring and maxlevel.

Syntax

r = reprlib.Repr(); r.maxlist = 3; r.repr(obj)

The limits are instance attributes; passing them to the constructor works only since 3.12, before that only by assignment. An abbreviated result cannot be turned back into an object with eval: the ellipsis is right in the text. For your own types add a method repr_<type name>, e.g. repr_MyClass(self, obj, level).

Examples

import reprlib
r = reprlib.Repr()
r.maxlist = 3
r.maxlevel = 1
print(r.repr(list(range(10))))  # → [0, 1, 2, ...]
print(r.repr([[1, [2]], 3]))  # → [[...], 3]
# Python 3.12+
import reprlib
r = reprlib.Repr(maxstring=10)
print(r.repr('abcdefghijklmnop'))  # → 'ab...nop'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy