4

It was my understanding that python will print the repr of the output, but this is apparently not always the case. For example:

In ipython:

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

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

In python:

>>> type([])
<type 'list'>
>>> set([3,1,2])
set([1, 2, 3])

What transformation does ipython apply on the output?

4

1 回答 1

9

代替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'

IPython.lib.pretty.pprint函数打印对象的表示:

>>> from IPython.lib.pretty import pprint
>>> pprint(type([]))
list

IPython 使用自己的漂亮打印机,因为标准 Pythonpprint模块“不允许开发人员提供自己的漂亮打印回调”。

于 2014-01-14T10:20:42.337 回答