8.6. Chaining Multiple Kinds of Operators

Chaining different kinds of operators in the same expression can produce unexpected bugs. For example, this (admittedly unrealistic) example uses the == and in operators in a single expression:

>>> False == False in [False]
True

This True result is surprising, because you would expect it to evaluate as either:

  • (False == False) in [False] , which is False .

  • False == (False in [False]) , which is also False .

But False == False in [False] isn’t equivalent to either of these expres- sions. Rather, it’s equivalent to (False == False) and (False in [False]) , just as 42 < spam < 99 is equivalent to (42 < spam) and (spam < 99) . This expression evaluates according to the following diagram:

(False == False) and (False in [False])

(True) and (False in [False])

(True) and (True)

True

The False == False in [False] expression is a fun Python riddle, but it’s unlikely to come up in any real-world code.