Search code examples
pythoncomparisonoperator-keywordcomparison-operators

Chaining "is" operators


Does python support chaining is operators, such as the following?

a = None
b = None
a is b is None

This outputs True, some doc references would be nice.


Solution

  • Yes. Any operators classified as comparisons can be chained. From the language reference:

    Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.

    The comparison operators are <, >, ==, >=, <=, <> (a little-used synonym for !=, gone in Python 3), !=, is, is not, in, and not in.