Boolean confusion

Alex Martelli aleax at aleax.it
Fri Oct 31 14:47:33 EST 2003


John Roth wrote:
   ...
>>  >>> 'a' in 'abc' == 1
>> False
   ...
> In other words, your first expression is equivalent
> to:
> 
> "a" in ("abc" == 1)

Nope: that would raise an exception rather than returning
False (try it!).

What's happening is *chaining* of relationals, just like
when you write e.g. a < b <= c.  In such cases the effect
is line (a < b) and (b <= c) except that b is only evaluated
once.  Similarly, the first expression above is equivalent
to
    ('a' in 'abc') and ('abc' == 1)


Alex





More information about the Python-list mailing list