Output difference between ipython and python

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?


Instead of repr or standard pprint module IPython uses IPython.lib.pretty.RepresentationPrinter.pretty method to print the output.

Module IPython.lib.pretty provides two functions that use RepresentationPrinter.pretty behind the scenes.

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 function prints the representation of an object:

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

IPython uses its own pretty printer because the standard Python pprint module "does not allow developers to provide their own pretty print callbacks."

链接地址: http://www.djcxy.com/p/28308.html

上一篇: Python在使用时打印内存地址而不是列表

下一篇: ipython和python之间的输出差异