0

According to short circuit rules, any time we have a True or ... statement it returns True, regardless of the rest of the statement. e.g., even if the rest of the statement throws an error, it would return True. Similarly, False and ... statement always returns False. I have also found this explanation online to sum it up.

So, when I tried to run this:

a = False or (5>"hello")
print(a)  

I had a TypeError because '>' is not supported between str and int.

b = True or (5>"hello")
print(b)

This one, however, prints True and it proves the short circuit rule. I did the same by applying for False and ... here:

c = False and (5>"hello")
print(c)

and this one prints False as expected.

So far, we have showcased the concept of short circuiting. But here is the problem:

d = False and False or True
print(d)

e = True or False and False
print(e)

If you run the code above, you would see that both d and e are True. Although we have a False and ... statement at the beginning for d, it seems to run the entire statement without short circuiting. So: False and False -> False, False or True -> True. Here I thought "Hm, ok. Maybe it is because I had 3 booleans". But when it came down to e the program seems to have a short circuit because if it hasn't, the result must have been: True or False -> True, True and False -> False.

Could you please explain why the program is short circuiting by e but not by d?

mercank
  • 3
  • 2
  • 3
    Does this answer your question? [Priority of the logical statements NOT AND & OR in python](https://stackoverflow.com/questions/16679272/priority-of-the-logical-statements-not-and-or-in-python) – Joseph Sible-Reinstate Monica May 03 '20 at 03:03

1 Answers1

1

and has a higher precedence than or, so your code is equivalent to this:

d = (False and False) or True
print(d)

e = True or (False and False)
print(e)

For it to work like you expect, you have to change d to this:

d = False and (False or True)
Alejandro De Cicco
  • 1,216
  • 3
  • 17
  • Thank you for the response! But then I have a follow up question: h = True or (5>"hello") and (5>"hello") print(h) According to the precedence, the AND part should be executed first and therefore it should throw an error. But this one prints True. – mercank May 03 '20 at 03:15
  • No, according to the precedence, that code is equal to `h = True or ((5>"hello") and (5>"hello"))`. As you can see, the left part is evaluated first. Since it is `True`, it short circuits and returns without evaluating the right part – Alejandro De Cicco May 03 '20 at 03:17
  • Don't the brackets have higher precedence? Like you said before, AND has higher precedence and you put the AND into brackets which means AND will evaluated first? So here, if you put them into brackets `(5>"hello") and (5>"hello")` should be evaluated first – mercank May 03 '20 at 03:24
  • What is happening here is that `(5>"hello") and (5>"hello")` is inside a bigger expression: `True or ((5>"hello") and (5>"hello"))`. That is how python interprets it. – Alejandro De Cicco May 03 '20 at 03:27
  • `and` has a higher precedence than `or` when they are at the same level in the same expression – Alejandro De Cicco May 03 '20 at 03:28
  • Well, in what I wrote at the first place they were at the same level: `h = True or (5>"hello") and (5>"hello") print(h) ` . So when you said that AND had higher precedence and put the AND part into brackets, I thought you mean that part would be evaluated first but you say that python first checks the outer expression and there `True or ...` part is short circuiting (so that there are no errors) have I understood correctly? Btw thanks for the explanation and your time. – mercank May 03 '20 at 03:46