Python 2和Python 3中type()函数的区别

Nit*_* MN 5 python python-2.7 python-3.x

在Python 2中尝试以下脚本时,

a = 200  
print type(a)   
Run Code Online (Sandbox Code Playgroud)

它的输出是

<type 'int'>
Run Code Online (Sandbox Code Playgroud)

在Python 3中,脚本

a = 200
print (type(a))
Run Code Online (Sandbox Code Playgroud)

它的输出是,

<class 'int'> 
Run Code Online (Sandbox Code Playgroud)

可能是什么原因?

Mar*_*ers 7

在昔日的日子里,内置类型如intdictlist它们构建的类型非常不同class.例如,您不能将内置类型子类化.

渐渐地,在连续的Python 2.x版本中,class类型和内置类型之间的差异已经被侵蚀; 在Python 2.2中引入新式类(继承自己object)是一个这样的(主要)步骤.请参阅Python 2.2中的统一类型和类.

删除type内置类型表示的使用只是该过程的最后一步.没有理由再使用这个名字type了.

换句话说,在Python 2.7和3.x之间,这是一个美化改变,仅此而已.