reprlib
— Alternate repr()
implementation替代repr()
实现¶
repr()
implementationSource code: Lib/reprlib.py
The reprlib
module provides a means for producing object representations with limits on the size of the resulting strings. reprlib
模块提供了一种生成对象表示的方法,并限制了生成字符串的大小。This is used in the Python debugger and may be useful in other contexts as well.这在Python调试器中使用,在其他上下文中也可能有用。
This module provides a class, an instance, and a function:该模块提供了一个类、一个实例和一个函数:
-
class
reprlib.
Repr
¶ 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.repr()
的函数时有用的格式化服务;添加了不同对象类型的大小限制,以避免生成过长的表示。
-
reprlib.
aRepr
¶ This is an instance of这是Repr
which is used to provide therepr()
function described below.Repr
的一个实例,用于提供下面描述的repr()
函数。Changing the attributes of this object will affect the size limits used by更改此对象的属性将影响repr()
and the Python debugger.repr()
和Python调试器使用的大小限制。
-
reprlib.
repr
(obj)¶ This is the这是repr()
method ofaRepr
.aRepr
的repr()
方法。It returns a string similar to that returned by the built-in function of the same name, but with limits on most sizes.它返回一个类似于同名内置函数返回的字符串,但对大多数大小有限制。
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__()
的递归调用,并替换占位符字符串。
-
@
reprlib.
recursive_repr
(fillvalue='...')¶ Decorator for__repr__()
methods to detect recursive calls within the same thread.__repr__()
方法的装饰器,用于检测同一线程内的递归调用。If a recursive call is made, the fillvalue is returned, otherwise, the usual如果进行递归调用,则返回fillvalue,否则,将进行通常的__repr__()
call is made.__repr__()
调用。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'>New in version 3.2.版本3.2中新增。
Repr
Objects对象¶
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.实例提供了几个属性,这些属性可用于为不同对象类型的表示提供大小限制,以及格式化特定对象类型的方法。
-
Repr.
maxlevel
¶ Depth limit on the creation of recursive representations.递归表示创建的深度限制。The default is默认值为6
.6
。
-
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.对命名对象类型表示的条目数的限制。The default is4
formaxdict
,5
formaxarray
, and6
for the others.maxdict
的默认值为4
,maxarray
的默认值为5
,其他为6
。
-
Repr.
maxlong
¶ Maximum number of characters in the representation for an integer.整数表示中的最大字符数。Digits are dropped from the middle.数字从中间删除。The default is默认值为40
.40
。
-
Repr.
maxstring
¶ 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
。
-
Repr.
maxother
¶ This limit is used to control the size of object types for which no specific formatting method is available on the此限制用于控制Repr
object.Repr
对象上没有特定格式方法的对象类型的大小。It is applied in a similar manner as它的应用方式与maxstring
.maxstring
类似。The default is默认值为20
.20
。
-
Repr.
repr
(obj)¶ The equivalent to the built-in等效于使用实例施加的格式的内置repr()
that uses the formatting imposed by the instance.repr()
。
-
Repr.
repr1
(obj, level)¶ Recursive implementation used byrepr()
.repr()
使用的递归实现。This uses the type of obj to determine which formatting method to call, passing it obj and level.这使用obj的类型来确定要调用的格式化方法,并将其传递给obj和level。The type-specific methods should call特定于类型的方法应该调用repr1()
to perform recursive formatting, withlevel - 1
for the value of level in the recursive call.repr1()
来执行递归格式化,递归调用中的level值为level - 1
。
-
Repr.
repr_TYPE
(obj, level) Formatting methods for specific types are implemented as methods with a name based on the type name.特定类型的格式化方法实现为具有基于类型名称的名称的方法。In the method name, TYPE is replaced by在方法名称中,类型替换为'_'.join(type(obj).__name__.split())
.'_'.join(type(obj).__name__.split())
。Dispatch to these methods is handled by对这些方法的调度由repr1()
.repr1()
处理。Type-specific methods which need to recursively format a value should call需要递归格式化值的特定于类型的方法应调用self.repr1(subobj, level - 1)
.self.repr1(subobj, level - 1)
。
Subclassing Repr Objects子类化Repr对象¶
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. Repr.repr1()
使用动态调度允许Repr
的子类添加对其他内置对象类型的支持,或修改对已支持类型的处理。This example shows how special support for file objects could be added:此示例显示了如何添加对文件对象的特殊支持:
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>'