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

与世无争的帅哥 提交于 2019-11-26 11:31:31

问题


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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/46288616/is-pythons-order-of-evaluation-of-function-arguments-and-operands-deterministic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!