Tony Stark
Tony Stark

Reputation: 23

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. The type() function got it right but why does the isinstance() function says that arg is an int? Is this a bug?

NOTE: I'm using Python 3.8.5

Upvotes: 1

Views: 913

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95883

Because bool objects are int objects, in other words:

>>> issubclass(bool, int)
True

Or, put it another way:

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

Upvotes: 4

Related Questions