[Tutor] comparison bug in python (or do I not get it?)
Ricardo Aráoz
ricaraoz at gmail.com
Sat Mar 1 17:15:23 CET 2008
Hans Fangohr wrote:
> Hi Kent,
>
>> Hans Fangohr wrote:
>>
>>> In [2]: 2 in [1,2,3] == True
>>> Out[2]: False
>>>
>>> Why does [2] return False? Would people agree that this is a bug?
>> No, not a bug. Don't be too quick to blame your tools!
>
> That's good news. I'd be worried if this wasn't the desired behaviour
> -- I just hadn't understood the logic.
>
>> The equivalent expression is
>> In [1]: (2 in [1,2,3]) and ([1,2,3]==False)
>> Out[1]: False
>
> Ah -- that's the way to read it!
>
>> 'in' is considered a comparison operator and can be chained with other
>> comparisons. For a clearer example, consider
>> In [2]: 2 < 3 < 4
>> Out[2]: True
>>
>> which is not the same as
>> In [3]: 2 < (3 < 4)
>> Out[3]: False
>>
>> or
>> In [4]: (2 < 3) < 4
>> Out[4]: True
>>
>> It is equivalent to
>> In [5]: (2 < 3) and (3 < 4)
>> Out[5]: True
>>
>
> Well explained -- makes perfect sense now.
>
Just one further question :
>>> 1 == True
True
>>> 5 == True
False
and yet
>>> if 5 : print 'True'
True
I thought a non-zero or non-empty was evaluated as True. Now in the 5 ==
True line I'm not saying "5 is True", shouldn't it evaluate just like
the "if" statement?
More information about the Tutor
mailing list