"0 in [True,False]" returns True
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Tue Dec 13 16:15:28 EST 2005
On Tue, 13 Dec 2005 09:46:30 +0000, Steve Holden wrote:
> Paul Rubin wrote:
>> Steve Holden <steve at holdenweb.com> writes:
>>
>>>The really interesting question your post raises, though, is "Why do
>>>you feel it's necessary to test to see whether a variable is a
>>>Boolean?".
>>
>>
>> What's the point of having Booleans, if you can't tell them from integers?
>
> Booleans are specifically defined as a subtype of int at the C level.
That's an implementation detail, and in fact an explicit decision made,
not a inescapable fact of programming. What you are saying is,
effectively, the point of having bools which are subclasses of ints is
that Guido wanted them that way.
> One might also ask "what's the point of having floats if you can't tell
> them from integers":
But you certainly can:
py> isinstance(True, int)
True
py> isinstance(0.0, int)
False
However, you can also ask:
py> isinstance(True, bool)
True
py> isinstance(1, bool)
False
or even:
py> type(True) == type(1)
False
but, I believe, using type() is frowned upon.
--
Steven
More information about the Python-list
mailing list