Chaining “is” operators

前端 未结 4 918
无人及你
无人及你 2020-11-28 15:49

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

a = None
b = None
a is b is None

This outputs True

4条回答
  •  醉话见心
    2020-11-28 16:29

    Yes. See comparison docs.

    Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

    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.

    What the is comparison operator does:

    The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

提交回复
热议问题