isinstance(False, int)

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Mar 5 13:07:29 EST 2010


On 2010年3月05日 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.
-- 
Steven


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /