reprlib — Alternaterepr() implementation

Source code:Lib/reprlib.py


Thereprlib module provides a means for producing object representationswith limits on the size of the resulting strings. This is used in the Pythondebugger and may be useful in other contexts as well.

This module provides a class, an instance, and a function:

classreprlib.Repr

Class which provides formatting services useful in implementing functionssimilar to the built-inrepr(); size limits for different object typesare added to avoid the generation of representations which are excessively long.

reprlib.aRepr

This is an instance ofRepr which is used to provide therepr() function described below. Changing the attributes of thisobject will affect the size limits used byrepr() and the Pythondebugger.

reprlib.repr(obj)

This is therepr() method ofaRepr. It returns a stringsimilar to that returned by the built-in function of the same name, but withlimits on most sizes.

In addition to size-limiting tools, the module also provides a decorator fordetecting recursive calls to__repr__() and substituting a placeholderstring instead.

@reprlib.recursive_repr(fillvalue="...")

Decorator for__repr__() methods to detect recursive calls within thesame thread. If a recursive call is made, thefillvalue is returned,otherwise, the usual__repr__() call is made. For example:

>>>fromreprlibimportrecursive_repr>>>classMyList(list):...@recursive_repr()...def__repr__(self):...return'<'+'|'.join(map(repr,self))+'>'...>>>m=MyList('abc')>>>m.append(m)>>>m.append('x')>>>print(m)<'a'|'b'|'c'|...|'x'>

New in version 3.2.

Repr Objects

Repr instances provide several attributes which can be used to providesize limits for the representations of different object types, and methodswhich format specific object types.

Repr.maxlevel

Depth limit on the creation of recursive representations. The default is6.

Repr.maxdict
Repr.maxlist
Repr.maxtuple
Repr.maxset
Repr.maxfrozenset
Repr.maxdeque
Repr.maxarray

Limits on the number of entries represented for the named object type. Thedefault is4 formaxdict,5 formaxarray, and6 forthe others.

Repr.maxlong

Maximum number of characters in the representation for an integer. Digitsare dropped from the middle. The default is40.

Repr.maxstring

Limit on the number of characters in the representation of the string. Notethat the “normal” representation of the string is used as the character source:if escape sequences are needed in the representation, these may be mangled whenthe representation is shortened. The default is30.

Repr.maxother

This limit is used to control the size of object types for which no specificformatting method is available on theRepr object. It is applied in asimilar manner asmaxstring. The default is20.

Repr.repr(obj)

The equivalent to the built-inrepr() that uses the formatting imposed bythe instance.

Repr.repr1(obj,level)

Recursive implementation used byrepr(). This uses the type ofobj todetermine which formatting method to call, passing itobj andlevel. Thetype-specific methods should callrepr1() to perform recursive formatting,withlevel-1 for the value oflevel in the recursive call.

Repr.repr_TYPE(obj,level)

Formatting methods for specific types are implemented as methods with a namebased on the type name. In the method name,TYPE is replaced by'_'.join(type(obj).__name__.split()). Dispatch to these methods ishandled byrepr1(). Type-specific methods which need to recursivelyformat a value should callself.repr1(subobj,level-1).

Subclassing Repr Objects

The use of dynamic dispatching byRepr.repr1() allows subclasses ofRepr to add support for additional built-in object types or to modifythe handling of types already supported. This example shows how special supportfor file objects could be added:

importreprlibimportsysclassMyRepr(reprlib.Repr):defrepr_TextIOWrapper(self,obj,level):ifobj.namein{'<stdin>','<stdout>','<stderr>'}:returnobj.namereturnrepr(obj)aRepr=MyRepr()print(aRepr.repr(sys.stdin))# prints '<stdin>'