Skip to main content
 首页 » 编程设计

python之复合相等性测试在 python 中短路了吗

2025年05月04日10Renyi-Fan

python 是否短路 bool 值,如 a==b==c,即,如果 a==b 为假,则不评估第二个相等性?复合相等性只是 a==b 和 b==c 的语法糖吗?

请您参考如下方法:

是的。来自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).

这适用于任何链式比较,与选择的比较运算符无关。