ipython和python之间的输出差异
我的理解是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方法来打印输出。
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使用自己的漂亮打印机,因为标准的Python pprint模块“不允许开发人员提供自己漂亮的打印回调”。
上一篇: Output difference between ipython and python
下一篇: Defining
