首页 > 解决方案 > __class__ 实现在 IPython 中是否针对 Python 进行了修改?

问题描述

我正在尝试将__class__Python 和 IPython 中的type()函数与两者中的函数进行比较,并想知道以下行为差异是否是因为__class__IPython wrt Python 中的修改。

Python

>>> [].__class__
<class 'list'>
>>> type([])
<class 'list'>

IPython

In [2]: [].__class__
Out[2]: list

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

但是对于 print 它似乎仍然是指 Python 的__class__

Python

>>> print(type([]))
<class 'list'>

IPython

In [4]: print(type([]))
<class 'list'>

标签: pythonipython

解决方案


这就是神奇发生的地方:

In [1] import sys
In [2] sys.displayhook
Out[2]: <IPython.terminal.prompts.RichPromptDisplayHook at 0x7f6c42b08190>

对比

>>> import sys
>>> sys.displayhook
<built-in function displayhook>

显示的对象(list<class 'list'>)是同一个对象。使用sys.displayhook,IPython 修改它在交互式 REPL 中的显示方式。


推荐阅读