Parth Jeet Parth Jeet - 5 months ago 40
Python Question

How to stop Python from short circuiting?

Python short circuits the logical operators.
for eg:

if False and Condition2:
#condition2 won't even be checked because the first condition is already false.

Is there a way to stop this behavior. I want it to check both the conditions and then perform the and operation(as done in c, c++ etc). It's useful when we are performing some operation along with the condition. Eg:

if a<p.pop() and b<p.pop():

One way can be checking the conditions before and then comparing the boolean values. But that would be wastage of memory.

Answer Source
if all([a < p.pop(), b < p.pop()])

This creates a list, which will be evaluated in its entirety, and then uses all to confirm that both values are truthy. But this is somewhat obscure and I'd rather suggest you write plain, easy to understand code:

a_within_limit = a < p.pop()
b_within_limit = b < p.pop()
if a_within_limit and b_within_limit:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download