ipython和python之间的输出差异 [英] Output difference between ipython and python

查看:153
本文介绍了ipython和python之间的输出差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,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之间的输出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆