In C++, there is no concept of left-to-right or right-to-left evaluation; however, the rules of associativity of operators do apply. For example:
W = (a + b) + c*d*e + f*g*h
In C++, we are assured that the operation c*d*e will be evaluated as (c*d)*e, due to the associativity of operators. The same thing applies to f*g*h. However, we do not know which subexpression will be evaluated first: (a + b), c*d*e or f*g*h. That arises because they are unsequenced relative to each other, and there is no defined order of evaluation in C++.
Regarding Python, the standard states in the topic 6.17:
Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation and conditional expressions, which group from right to left).
To me, it seems that “Operators in the same box group left to right […]” refers to associativity, not to left-to-right or right-to-left evaluation. This seems to be confirmed by the book Python (Chris Fehily):
Associativity determines the order of evaluation in an expression when adjacent operators have equal precedence. Python used left-to-right associativity for all operators except **(exponentiation), which groups from right to left.
Furthermore, in the book “Computer Science with Python Language Class – XI” (Dr. Jayant Sharma and Gurpreet Kaur):
1 + ((3 + 1) / (8 – 4) – 5)
The innermost parentheses are evaluated first. The expressions (3 + 1) and (8 – 4) are at the same depth, so they can be evaluated in either order.
From these examples and explanations, it seems that there is also no left-to-right or right-to-left evaluation order in Python.
However, in the book “A Functional Start to Computing with Python” (Ted Herman):
General Rules: Left-to-right evaluation, but look-ahead first, checking if a higher priority operation comes next; parentheses force evaluation order; evaluation work can be “queued up” due to operator priority.
2 – 3 – 4 * 2**5 + 1
Going left-to-right, the first operator is subtraction: does Python therefore immediately reduce 2-3 to -1? No! First, Python observes that the operator following 3 is another minus operator; now, since the operator on the left of 3 and on the right of 3 have the same priority, Python can safely reduce 2-3 to -1 and get
-1 – 4 * 2**5 + 1
as a partially reduced expression. The next operation to consider is reducing -1-4 to -5: can this be done? No! First, Python observes that the operator to the right of 4 is *. Comparing multiplication to minus in the priority table of Figure 7.1, we see that multiplication has higher priority. Hence, Python should reduce 4*2 before reducing the minus sign to the left of 4. So is that what Python does next? No! Before reducing 4*2, Python looks to the right of 2 and finds the operator **. […]
This seems to indicate that there is a left-to-right evaluation. From what I gather, there would be no way to know that 2 – 3 would be evaluated before (or after) 2**5. What am I missing here?
Finally, this site provides the following example:
100 + 200 / 10 – 3 * 10
Divide “/” will happen first. It has higher precedence than + and -. It has the same precedence as *, but higher associativity.
Again, an evaluation order seems to be used here, and it seems that the author points to associativity, but refers to evaluation order. From what I know, there is no rule in Python that assures that 200/10 will be evaluated before 3 * 10. Is that right?
To wrap-up, in the first example we have no way of knowing which subexpression will be evaluated first in C++: (a + b), c*d*e or f*g*h. Does the same apply to Python? Like C++, Python does not have the concept of left-to-right or right-to-left evaluation, but only associativity and precedence order?
4