I have a line of code in my script that has both these operators chained together. From the documentation reference BOOLEAN AND has a lower precedence than COMPARISON GREATER THAN. I am getting unexpected results here in this code:
>>> def test(msg, value):
... print(msg)
... return value
>>> test("First", 10) and test("Second", 15) > test("Third", 5)
First
Second
Third
True
I was expecting Second or Third test to happen before the fist one, since > operator has a higher precedence. What am I doing wrong here?
https://docs.python.org/3/reference/expressions.html#operator-precedence

10, is also True. Therefore,10 and 15 > 5 == 10 and (15 > 5) == 10 and True == True- Finwoodtest("First", 10) and test("Second", 15) > test("Third", 5)is equivalent totest("First", 10) and (test("Second", 15) > test("Third", 5))Also Python evaluatesandlazily - Alik0 and 0 > -1is different from(0 and 0) > -1. My guess is that while the values returned are evaluated in the correct order, the functions are not called in that order for some reason. What's interesting is that replacing 10,15,5 with 0,0,-1 only printsFirst. Maybe it's a compiler optimization - since in your case, as @Alik pointed out, both orders result in the same value. - pushkin