True == 1 weirdness

Blake T. Garretson blake.garretson at gmail.com
Wed Sep 16 08:16:21 EDT 2015


I am maintaining some old code where the programmer used 1 for True because booleans hadn't been added to Python yet.  I'm getting some weird behaviour, so I created some simple tests to illustrate my issue.

>>> 1 in {1:1}        #test1
True
>>> 1 in {1:1} == 1   #test2
False
>>> (1 in {1:1}) == 1 #test3
True
>>> 1 in ({1:1} == 1) #test4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
>>>

You can see that in the first test we get True as expected.  The second test yield False, because True does not equal 1, apparently.  Fair enough.  But then the third test yields True.  Weird.  Okay, so maybe it was evaluating the right side first?  So I put the parens over to the right and it fails, so that wasn't it.  So why do the second and third test differ?



More information about the Python-list mailing list