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?". regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.python.org/pycon/ -- http://mail.python.org/mailman/listinfo/python-list