"0 in [True,False]" returns True
Antoon Pardon
apardon at forel.vub.ac.be
Tue Dec 13 04:25:57 EST 2005
Op 2005-12-13, Steve Holden schreef <steve at holdenweb.com>:
> Pierre Quentel wrote:
>> Hi all,
>>
>> In some program I was testing if a variable was a boolean, with this
>> test : if v in [True,False]
>>
>> My script didn't work in some cases and I eventually found that for v =
>> 0 the test returned True
>>
>> So I changed my test for the obvious "if type(v) is bool", but I still
>> find it confusing that "0 in [True,False]" returns True
>>
>> By the way, I searched in the documentation what "obj in list" meant and
>> couldn't find a precise definition (does it test for equality or
>> identity with one of the values in list ? equality, it seems) ; did I
>> miss something ?
>>
> It actually uses the __contains__() method of the right-hand operand,
> and in the case of a list that will test for equality of the left-hand
> operand to one of the list elements. Since False == 0 that's why you see
> what you do.
>
> 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?".
I can give you one example. I have written a tube class. A tube behaves
like Queue but it has additional code so that it can be registed with
gtk in the same way as file descriptor can be registered with
io_add_watch. The way this is implemented is by registering an idle
handler when the tube is not empty and removing it when the tube is
empty. So I have a variable cb_src (for callback source) that can be
a boolean or an integer. The possible values are
False: Not registered by the user
True: Registered by the user but no nternal idle callback registerd
a number: gtk integer ID, from the registered idle callback handler.
--
Antoon Pardon
More information about the Python-list
mailing list