I’ve recently stumbled over this expression:
True == False in (False,)
It evaluates to False, but I don’t understand why.
True == False is False and False in (False,) is True, so both (to me) plausible possibilities
True == (False in (False,))
and
(True == False) in (False,)
evaluate to True, as I would have expected.
What is going wrong here?
I believe this is a corner case of Python’s comparison-operator chaining. It gets expanded to
which evaluates to
False.This behavior was intended to match conventional math notation (e.g.
x == y == zmeaning that all three are equal, or0 <= x < 10meaningxis in the range [0, 10)). Butinis also a comparison operator, giving the unexpected behavior.