reprlib
repr()
pprint — 数据美化打印机
pprint
源代码: Lib/reprlib.py
The reprlib module provides a means for producing object representations with limits on the size of the resulting strings. This is used in the Python debugger and may be useful in other contexts as well.
This module provides a class, an instance, and a function:
Class which provides formatting services useful in implementing functions similar to the built-in repr() ; size limits for different object types are added to avoid the generation of representations which are excessively long.
The keyword arguments of the constructor can be used as a shortcut to set the attributes of the Repr instance. Which means that the following initialization:
Repr
aRepr = reprlib.Repr(maxlevel=3)
相当于:
aRepr = reprlib.Repr() aRepr.maxlevel = 3
见章节 Repr 对象 for more information about Repr 属性。
Changed in version 3.12: Allow attributes to be set via keyword arguments.
This is an instance of Repr which is used to provide the repr() function described below. Changing the attributes of this object will affect the size limits used by repr() and the Python debugger.
这是 repr() 方法为 aRepr . It returns a string similar to that returned by the built-in function of the same name, but with limits on most sizes.
aRepr
In addition to size-limiting tools, the module also provides a decorator for detecting recursive calls to __repr__() and substituting a placeholder string instead.
__repr__()
Decorator for __repr__() methods to detect recursive calls within the same thread. If a recursive call is made, the fillvalue is returned, otherwise, the usual __repr__() call is made. For example:
>>> from reprlib import recursive_repr >>> class MyList(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'>
Added in version 3.2.
Repr instances provide several attributes which can be used to provide size limits for the representations of different object types, and methods which format specific object types.
This string is displayed for recursive references. It defaults to ... .
...
Added in version 3.11.
Depth limit on the creation of recursive representations. The default is 6 .
6
Limits on the number of entries represented for the named object type. The default is 4 for maxdict , 5 for maxarray ,和 6 for the others.
4
maxdict
5
maxarray
Maximum number of characters in the representation for an integer. Digits are dropped from the middle. The default is 40 .
40
Limit on the number of characters in the representation of the string. Note that the “normal” representation of the string is used as the character source: if escape sequences are needed in the representation, these may be mangled when the representation is shortened. The default is 30 .
30
This limit is used to control the size of object types for which no specific formatting method is available on the Repr object. It is applied in a similar manner as maxstring 。默认为 20 .
maxstring
20
If this attribute is set to None (the default), the output is formatted with no line breaks or indentation, like the standard repr() 。例如:
None
>>> example = [ ... 1, 'spam', {'a': 2, 'b': 'spam eggs', 'c': {3: 4.5, 6: []}}, 'ham'] >>> import reprlib >>> aRepr = reprlib.Repr() >>> print(aRepr.repr(example)) [1, 'spam', {'a': 2, 'b': 'spam eggs', 'c': {3: 4.5, 6: []}}, 'ham']
若 indent is set to a string, each recursion level is placed on its own line, indented by that string:
indent
>>> aRepr.indent = '-->' >>> print(aRepr.repr(example)) [ -->1, -->'spam', -->{ -->-->'a': 2, -->-->'b': 'spam eggs', -->-->'c': { -->-->-->3: 4.5, -->-->-->6: [], -->-->}, -->}, -->'ham', ]
设置 indent to a positive integer value behaves as if it was set to a string with that number of spaces:
>>> aRepr.indent = 4 >>> print(aRepr.repr(example)) [ 1, 'spam', { 'a': 2, 'b': 'spam eggs', 'c': { 3: 4.5, 6: [], }, }, 'ham', ]
3.12 版添加。
相当于内置 repr() that uses the formatting imposed by the instance.
Recursive implementation used by repr() . This uses the type of obj to determine which formatting method to call, passing it obj and level . The type-specific methods should call repr1() to perform recursive formatting, with level - 1 for the value of level in the recursive call.
repr1()
level - 1
Formatting methods for specific types are implemented as methods with a name based on the type name. In the method name, TYPE 被替换通过 '_'.join(type(obj).__name__.split()) . Dispatch to these methods is handled by repr1() . Type-specific methods which need to recursively format a value should call self.repr1(subobj, level - 1) .
'_'.join(type(obj).__name__.split())
self.repr1(subobj, level - 1)
The use of dynamic dispatching by Repr.repr1() allows subclasses of Repr to add support for additional built-in object types or to modify the handling of types already supported. This example shows how special support for file objects could be added:
Repr.repr1()
import reprlib import sys class MyRepr(reprlib.Repr): def repr_TextIOWrapper(self, obj, level): if obj.name in {'<stdin>', '<stdout>', '<stderr>'}: return obj.name return repr(obj) aRepr = MyRepr() print(aRepr.repr(sys.stdin)) # prints '<stdin>'
<stdin>
enum — 支持枚举
enum
键入搜索术语或模块、类、函数名称。