9

C doesn't guarantee any evaluation order so a statement like f(g1()+g2(), g3(), g4()) might execute g1(), g2(), g3(), and g4() in any order (although f() would be executed after all of them)

What about Python? My experimentation for Python 2.7 shows that it appears to be left-to-right order of evaluation but I wonder if this is specified to be the case.

Test program:

def somefunc(prolog, epilog):
    print prolog
    def f(a, b, *args):
        print epilog
    return f        

def f(text):
    print text
    return 1

somefunc('f1','f2')(f('hey'),f('ho'),f('hahaha'),f('tweedledee')+f('tweedledum'))

which prints

f1
hey
ho
hahaha
tweedledee
tweedledum
f2
smci
  • 32,567
  • 20
  • 113
  • 146
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • 1
    @DYZ I asked about function arguments *and* operator evaluation, not just function arguments. – Jason S Sep 18 '17 at 21:48
  • [careful](https://stackoverflow.com/questions/40226063/set-literal-gives-different-result-from-set-function-call) about some [bugs](https://bugs.python.org/issue11205) though – wim Sep 18 '17 at 21:52
  • I get a bad feeling about code where an argument's value is different depending upon the order it is in the argument list, so I tend to avoid situations where left-to-right order is significant. But it's good to know what it is, and for Python it is left to right, as others have pointed out. – RufusVS Sep 18 '17 at 22:00
  • Near-duplicates [Is python assignment strictly evaluated right to left? \[duplicate\]](https://stackoverflow.com/questions/29753335/is-python-assignment-strictly-evaluated-right-to-left), [Is the right-hand side of an assignment always evaluated before the assignment?](https://stackoverflow.com/questions/27112647/is-the-right-hand-side-of-an-assignment-always-evaluated-before-the-assignment) – smci Aug 17 '18 at 22:35

2 Answers2

14

Yes, left to right evaluation order is guaranteed, with the exception of assignments. That's documented here (py2, py3):

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2

If the language were not making some choice about this, the evaluation of one argument could mutate another argument and result in unspecified behaviour, so all implementations of Python must follow this spec.

Community
  • 1
  • 1
wim
  • 338,267
  • 99
  • 616
  • 750
4

https://docs.python.org/3/reference/expressions.html#evaluation-order:

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2

The same is true in Python 2.

jwodder
  • 54,758
  • 12
  • 108
  • 124