据我了解,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
方法打印输出 http://ipython.org/ipython-doc/dev/api/generated/IPython.lib.pretty.html#IPython.lib.pretty.RepresentationPrinter.
Module 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 使用自己的漂亮打印机,因为标准 Pythonpprint
模块“不允许开发人员提供自己漂亮的打印回调。”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)