首页 > 解决方案 > 为什么 type(x) 和 print(type(x) 在 python 中显示的结果略有不同?

问题描述

x = [1,2,3]

然后在ipython,我得到以下

> In [8]: type(x) 
Out[8]: list
> 
> In [9]: print(type(x)) 
<class 'list'>

为什么不 print(type(x)) 只是打印列表,而不是额外的类的东西?

一个相关的问题是,如何显示输出

type of x is; list

print(' type of x is: ', type(x)) 不这样做是因为我得到了额外的课程内容,例如

In [2]: print(' type of x is: ', type(x))
 type of x is:  <class 'list'>

标签: pythontypes

解决方案


正如 pwxcoo 所说,这为Ipython您做了一些额外的工作,请在普通的 python shell 中尝试它,除非您明确显示,否则它不会显示任何内容print()


推荐阅读