gpt4 book ai didi

python - ipython和python之间的输出差异

转载 作者:行者123 更新时间:2023-12-03 20:50:04 24 4
gpt4 key购买 nike

据我了解,python 将打印 repr的输出,但显然并非总是如此。例如:

在 ipython 中:

In [1]: type([])
Out[1]: list

In [2]: set([3,1,2])
Out[2]: {1, 2, 3}

在 python 中:
>>> type([])
<type 'list'>
>>> set([3,1,2])
set([1, 2, 3])

ipython 对输出应用什么转换?

最佳答案

而不是 repr或标准 pprint模块 IPython 使用 IPython.lib.pretty.RepresentationPrinter.pretty方法 print the output .

模块 IPython.lib.pretty提供两个使用 RepresentationPrinter.pretty 的函数在幕后。
IPython.lib.pretty.pretty函数返回对象的字符串表示:

>>> from IPython.lib.pretty import pretty
>>> pretty(type([]))
'list'
IPython.lib.pretty.pprint函数打印对象的表示:
>>> from IPython.lib.pretty import pprint
>>> pprint(type([]))
list

IPython 使用自己的 pretty-print ,因为标准 Python pprint模块“不允许开发人员提供自己的 pretty-print 回调。”

关于python - ipython和python之间的输出差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63353783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com