_safe_repr 和 "python -m pprint" 中的 pformat 是什么意思?

what do _safe_repr and pformat in "python -m pprint" mean?

它们是否与 python 解释器或 linux 发行版的标准输出有关
如果您对此感兴趣,请给我一个流程的解释

$type cd | python -m pprint 

_safe_repr: 4.332890303998283
pformat: 9.275150911998935

来自pprint.py

if __name__ == "__main__":
    _perfcheck()

...其中 运行s:

def _perfcheck(object=None):
    import time
    if object is None:
        object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000
    p = PrettyPrinter()
    t1 = time.perf_counter()
    _safe_repr(object, {}, None, 0)
    t2 = time.perf_counter()
    p.pformat(object)
    t3 = time.perf_counter()
    print("_safe_repr:", t2 - t1)
    print("pformat:", t3 - t2)

因此,当您 运行 python -m pprint(调用 pprint 模块作为 __main__)时,_safe_repr() 的性能(包装和记录以供外部使用 saferepr()) and the performance of pformat() 正在与 [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000 创建的示例对象进行比较。


引用文档区分这两个函数的含义和用途:

pformat() Format a Python object into a pretty-printed representation.

saferepr() Generate a 'standard' repr()-like value, but protect against recursive data structures.