简体   繁体   English

为什么 isinstance() function 不起作用?

[英]Why is the isinstance() function not working?

For example:例如:

def test(arg = False):
    return arg, type(arg), isinstance(arg, int)

print(test())

will result in:将导致:

False, <class: 'bool', True>

The arg variable is False which is obviously a boolean . arg变量是False ,这显然是boolean The type() function got it right but why does the isinstance() function says that arg is an int ? type() function 做对了,但为什么isinstance() function 说argint Is this a bug?这是一个错误吗?

NOTE: I'm using Python 3.8.5注意:我正在使用 Python 3.8.5

Because bool objects are int objects, in other words :因为bool对象是int对象,换句话说

>>> issubclass(bool, int)
True

Or, put it another way:或者,换一种说法:

>>> bool.mro()
[<class 'bool'>, <class 'int'>, <class 'object'>]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM