1

I have multiple expensive condition to make and I am wondering if this:

if False and False and True and False:
    print("you will never see this")

is longer than this in time computing:

if False:
    if False:
        if True:
            if False:
                print("you will never see this")

Will python stop the first time it see a wrong condition or will it compute every verification before deciding ?

  • Python evaluates that *lazily*, although given that you have hard-coded conditions it all has to be *parsed* anyway. But e.g. `foo() and bar()` won't call bar if foo returns false-y. – jonrsharpe Jan 06 '21 at 11:11

1 Answers1

1

It doesn't matter, because Python supports short-circuit evaluation:

Does Python support short-circuiting?

Johan
  • 477
  • 4
  • 8
  • Thanks for your answers, sorry for the duplicate, I didn't find the rights keywords. (but I think these keywords can still be useful). So the answer is "yes Python stop at the first wrong condition". –  Jan 06 '21 at 11:15