问题描述
例如:
def test(arg = False):
return arg,type(arg),isinstance(arg,int)
print(test())
将导致:
False,<class: 'bool',True>
arg
变量是 False
,它显然是一个 boolean
。 type()
函数是正确的,但为什么 isinstance()
函数说 arg
是 int
?这是一个错误吗?
注意:我使用的是 Python 3.8.5
解决方法
因为 bool
对象是 int
对象,换句话说:
>>> issubclass(bool,int)
True
或者换一种说法:
>>> bool.mro()
[<class 'bool'>,<class 'int'>,<class 'object'>]