如果value1==value2在python中不是None,compare如何工作?

2024-04-23 07:03:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现了

a == b is not None

比较a == b,如果为真,则执行b is not None。你知道吗

(a == b) is not None  

以及

a == (b is not None)

我在哪里可以找到更多关于这种行为的信息? 它非常简单,但我希望True is not None被执行


Tags: none信息trueisnot
1条回答
网友
1楼 · 发布于 2024-04-23 07:03:33

语言参考6.10. Comparisons中记录了这一点:

Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics:

comparison    ::=  or_expr (comp_operator or_expr)*
comp_operator ::=  "<" | ">" | "==" | ">=" | "<=" | "!="
                   | "is" ["not"] | ["not"] "in"

Comparisons yield boolean values: True or False.

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.

Note that a op1 b op2 c doesn’t imply any kind of comparison between a and c, so that, e.g., x < y > z is perfectly legal (though perhaps not pretty).

==is not都是比较运算符,因此它们是如上所述的链。你知道吗

相关问题 更多 >