0

What is the execution flow when there are multiple statements with multiple or operator to be checked in a if condition.

def fun1():
    # heavy computation
    # returns True/False

#similarly
def fun2(); # heavy computation returns True/False
def fun3(); # heavy computation returns True/False

if fun1() or fun2() or fun3():
    #execute

Does python executes one function after other (i.e. fun1, fun2, fun3) sequentially? Will the execution stops once one of the function returns true and enters the if statement?

I am trying get the optimized code version for the above condition.

  • 1
    Yes, this is called shortcut evaluation. Performed from left to right. It is in fact harder to force execution of all three functions ! –  Nov 30 '22 at 11:42
  • Also, see this related question, which also links to the Python docs on this topic - https://stackoverflow.com/questions/2580136/does-python-support-short-circuiting – Zailef Nov 30 '22 at 11:47
  • 1
    The functions will be executed sequentially (left to right) but execution/evaluation will cease when (if?) either fun1 or fun2 returns True (or any truthy value) – DarkKnight Nov 30 '22 at 11:49

0 Answers0