python - ipython 和 python 之间的输出差异

标签 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])

ipython 对输出应用什么转换?

最佳答案

IPython 使用IPython.lib.pretty.RepresentationPrinter.pretty 方法来代替repr 或标准pprint 模块print the output .

模块 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 使用自己的 pretty-print ,因为标准 Python pprint 模块“不允许开发人员提供他们自己的 pretty-print 回调。”

关于python - ipython 和 python 之间的输出差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21110915/

相关文章:

swift - 在溢出的情况下 double 如何快速截断

python - 具有不同 x 数据的堆栈图

python - 在检测到的轮廓opencv-python中查找颜色

python - IPython 和 Jupyter 自动完成功能不起作用

node.js - 评估实时进程

ruby - 使用 RSpec 和线程在 Ruby 中测试 REPL

python - Jupyter笔记本找不到模块

python - 初次提交模型表单时 Django 内联表单集是否可行?

python - 从 iPython 运行 ga.read_ga 的问题

python - ipython pandas plot不显示