如何在Python中停止短路?

Par*_*eet 2 python short-circuiting logical-operators

Python使逻辑运算符短路.例如:

if False and Condition2:
    #condition2 won't even be checked because the first condition is already false.
Run Code Online (Sandbox Code Playgroud)

有没有办法阻止这种行为.我希望它检查两个条件,然后执行和操作(如c,c ++等).当我们与条件一起执行某些操作时,它很有用.例如:

if a < p.pop() and b < p.pop():
Run Code Online (Sandbox Code Playgroud)

一种方法是先检查条件,然后比较布尔值.但这会浪费记忆力.

dec*_*eze 9

if all([a < p.pop(), b < p.pop()])
Run Code Online (Sandbox Code Playgroud)

这将创建一个列表,该列表将完整地进行评估,然后用于all确认两个值都是真实的.但这有点模糊,我宁愿建议您编写简单易懂的代码:

a_within_limit = a < p.pop()
b_within_limit = b < p.pop()
if a_within_limit and b_within_limit:
Run Code Online (Sandbox Code Playgroud)