问题描述
据我了解,python将打印输出的repr
,但显然并非总是如此.例如:
It was my understanding that python will print the repr
of the output, but this is apparently not always the case. For example:
在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对输出应用什么转换?
What transformation does ipython apply on the output?
推荐答案
IPython使用IPython.lib.pretty.RepresentationPrinter.pretty
方法代替repr
或标准pprint
模块打印输出.
Instead of repr
or standard pprint
module IPython uses IPython.lib.pretty.RepresentationPrinter.pretty
method to print the output.
模块IPython.lib.pretty
提供了两个在后台使用RepresentationPrinter.pretty
的功能.
Module IPython.lib.pretty
provides two functions that use RepresentationPrinter.pretty
behind the scenes.
IPython.lib.pretty.pretty
函数返回对象的字符串表示形式:
IPython.lib.pretty.pretty
function returns the string representation of an object:
>>> from IPython.lib.pretty import pretty
>>> pretty(type([]))
'list'
IPython.lib.pretty.pprint
函数打印对象的表示形式:
IPython.lib.pretty.pprint
function prints the representation of an object:
>>> from IPython.lib.pretty import pprint
>>> pprint(type([]))
list
IPython使用其自己的漂亮打印机,因为标准的Python pprint
模块不允许开发人员提供自己的漂亮打印回调."
IPython uses its own pretty printer because the standard Python pprint
module "does not allow developers to provide their own pretty print callbacks."
这篇关于ipython和python之间的输出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!