Search code examples
pythonboolean-logicoperator-precedenceshort-circuiting

Python `or`, `and` operator precedence example


I cannot produce example in Python which shows Boolean operator precedence rules combined with short circuit evaluation. I can show operator precedence using:

print(1 or 0 and 0)  # Returns 1 because `or` is evaluated 2nd.

But the issue with short circuiting shows up when I change it to this:

def yay(): print('yay'); return True
def nay(): print('nay')
def nope(): print('nope')
print(yay() or nay() and nope())  # Prints "yay\nTrue"

For each of 4 possibilities when expression before or is True it is the only evaluated expression. If operator precedence works this should print "nay\nnope\nyay\nTrue" or "nay\nyay\nTrue", with short circuiting, because and should be evaluated 1st.

What comes to mind from this example is that Python reads boolean expression from left to right and ends it when result is known regardless of operator precedence.

Where is my error or what am I missing? Please give an example where it's visible that and is evaluated 1st and it isn't due to code being interpreted from left to right.


Solution

  • You are confusing operator precedence and evaluation order.

    The expression r = x or y and z is not evaluated as tmp = y and z; r = x or tmp, but just as r = x or (y and z). This expression is evaluated from left to right, and if the result of the or is already decided, then (y and z) will not be evaluated at all.

    Note that it would be different if or and and were functions; in this case, the parameters of the functions would be evaluated before the function itself is called. Hence, operator.or_(yay(), operator.and_(nay(), nope())) prints yay, nay and nope i.e. it prints all three, but still in order from left to right.

    You can generalize this to other operators, too. The following two expressions will yield different results due to the different operator precedence (both implicit and explicit by using (...)), but the functions are called from left to right both times.

    >>> def f(x): print(x); return x
    >>> f(1) + f(2) * f(3) / f(4) ** f(5) - f(6)         # 1 2 3 4 5 6 -> -4.99
    >>> (f(1) + f(2)) * (((f(3) / f(4)) ** f(5)) - f(6)) # 1 2 3 4 5 6 -> -17.29
    

    As pointed out in comments, while the terms in between operations are evaluated from left to right, the actual operations are evaluated according to their precedence.

    class F:
        def __init__(self,x): self.x = x
        def __add__(self, other): print(f"add({self},{other})"); return F(self.x+other.x)
        def __mul__(self, other): print(f"mul({self},{other})"); return F(self.x*other.x)
        def __pow__(self, other): print(f"pow({self},{other})"); return F(self.x**other.x)
        def __repr__(self): return str(self.x)
    def f(x): print(x); return F(x)
    

    This way, the expression f(1) + f(2) ** f(3) * f(4) is evaluated as 1, 2, 3, pow(2,3), 4, mul(8,4), add(1,32), i.e. terms are evaluated left-to-right (and pushed on a stack) and expressions are evaluated as soon as their parameters are evaluated.