"0 in [True,False]" returns True

Steven Bethard steven.bethard at gmail.com
Mon Dec 12 19:34:26 EST 2005


Pierre Quentel wrote:
> 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

This seems like a strange test.  What is this code trying to do?

If you're sure you have to do this, I would do either:

     if isinstance(v, bool):
         ...

or

     if v is True or v is False:
         ...

But it really seems like this code is trying to code some other language 
in Python...

STeVe



More information about the Python-list mailing list