isinstance() 与 type() 区别:
-
type() 不会认为子类是一种父类类型,不考虑继承关系。
-
isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
程序:
print(type(int))
print(type(1))
print(type(1)==int)
输出:
<type 'type'>
<type 'int'>
True
说明:1的类型是int。type()
返回值是什么类型,输入print(type(type(1))),看到输出<type 'type'>,可知type()返回一种特殊的类型type类型。然后我们可以type(int)或str,发现都为type类型,所以可以利用type(1)==int判断是否为int类型。