ipython和python之间的输出差异
据我所知,python将打印输出的ipython和python之间的输出差异,python,ipython,read-eval-print-loop,repr,Python,Ipython,Read Eval Print Loop,Repr,据我所知,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]) >>类型([] >>>集合([3,1,2]) 集合([1,2,3]) i
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])
>>类型([]
>>>集合([3,1,2])
集合([1,2,3])
ipython在输出上应用了什么转换?而不是
repr
或标准pprint
模块ipython使用ipython.lib.pretty.RepresentationPrinter.pretty
方法
模块IPython.lib.pretty
提供了两个在后台使用RepresentationPrinter.pretty
的函数
IPython.lib.pretty.pretty
函数返回对象的字符串表示形式:
>>> from IPython.lib.pretty import pretty
>>> pretty(type([]))
'list'
>>> from IPython.lib.pretty import pprint
>>> pprint(type([]))
list
IPython.lib.pretty.pprint函数打印对象的表示:
>>> from IPython.lib.pretty import pretty
>>> pretty(type([]))
'list'
>>> from IPython.lib.pretty import pprint
>>> pprint(type([]))
list
IPython使用自己的pretty打印机,因为标准Pythonpprint
模块“不允许开发人员提供自己的pretty打印回调。”IPython1.1.0
在Python2.7.5
上是否有文档记录了IPython.lib.pretty.pretty
确实是使用的函数?我做了IPython.lib.pretty.pretty=lambda*args,**kwargs:“土豆”
并且IPython内的输出没有变化。@wim,很好,我编辑了我的答案IPython.lib.pretty.RepresentationPrinter.pretty=lambda*args,**kwargs:sys.stdout.write('12')
将为任何输入打印12。请注意,只有启用了%pprint
魔术时才会出现这种情况。我在默认情况下将其关闭,并获取OP在常规Python提示符下看到的行为。