I am currently working on understanding types and how to check for them. I'm now stuck on this:
input_variable = True
print(type(input_variable))
print(isinstance(input_variable, bool))
print(isinstance(input_variable, int))
print(isinstance(input_variable, str))
print(isinstance(input_variable, float))
This outputs:
<class 'bool'>
True
True
False
False
I'm expecting the variable to be a bool, which is confirmed by the first type()
statement. I'm trying to understand why both the isinstance()
checks for bool and int return true. So I tried to cast my original variable to an int with
int(input_variable)
which gives me 1. I sort of understand this with True = 1 and False = 0, but I'm still confused as to why I don't have to explicitly cast the variable to an int to have isinstance()
recognize it as such.