Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Python's order of evaluation of function arguments and operands deterministic (+ where is it documented)?

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
like image 629
Jason S Avatar asked Sep 18 '17 21:09

Jason S


People also ask

Which is evaluated first and or or python?

Order of Evaluation In Python, the left operand is always evaluated before the right operand. That also applies to function arguments. Python uses short circuiting when evaluating expressions involving the and or or operators.

Does the order of arguments matter in Python?

The order of positional arguments matters Arguments need to be passed in the exact same order as the order of the parameters that have been declared in the function's definition. It works like variable assignment. The first argument is the value of the first parameter in the function's definition.

What are the 3 types of arguments in Python?

Hence, we conclude that Python Function Arguments and its three types of arguments to functions. These are- default, keyword, and arbitrary arguments.


1 Answers

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.

like image 124
wim Avatar answered Nov 10 '22 09:11

wim