Take a look at this:
>>> def f():
... return (2+3)*4
...
>>> dis(f)
2 0 LOAD_CONST 5 (20)
3 RETURN_VALUE
Evidently, the compiler has pre-evaluated (2+3)*4
, which makes sense.
Now, if I simply change the order of the operands of *
:
>>> def f():
... return 4*(2+3)
...
>>> dis(f)
2 0 LOAD_CONST 1 (4)
3 LOAD_CONST 4 (5)
6 BINARY_MULTIPLY
7 RETURN_VALUE
The expression is no longer fully pre-evaluated! What is the reason for this? I am using CPython 2.7.3.