isinstance(False, int)

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Mar 5 13:54:10 EST 2010


Steven D'Aprano wrote:
> On Fri, 05 Mar 2010 18:14:16 +0100, mk wrote:
>
>   
>>>>> isinstance(False, int)
>>>>>           
>> True
>>  >>>
>>  >>> isinstance(True, int)
>> True
>>
>> Huh?
>>     
>
> Yes. Do you have an actual question?
>
>
>   
>>  >>> issubclass(bool, int)
>> True
>>
>> Huh?!
>>     
>
> Exactly.
>
> Bools are a late-comer to Python. For historical and implementation 
> reasons, they are a subclass of int, because it was normal for people to 
> use 0 and 1 as boolean flags, and so making False == 0 and True == 1 was 
> the least likely to break code.
>
> E.g. back in the day, you would have something like:
>
> {2:None}.has_key(2) -> 1
>
> So folks would do:
>
> print "The key is", ["missing", "present"][d.has_key(key)]
>
> Which still works even now that has_key returns True or False rather than 
> 1 or 0.
>
>
>   
Despite there are good reasons for bool to be int, the newcomer 'wtf' 
reaction at first glance is legitimate.
Starting python from scratch, booleans would have not been a subclass of 
int (just guessing though), 'cause it makes no sense from a design POV.
Booleans are not ints, 0 does not *mean* False and veracity is not 
quantification.


JM



More information about the Python-list mailing list