Search code examples
pythonassignment-operatorevaluation

Argument evaluation order


Is the python argument evaluation order defined behaviour? And is the order of evaluation the same as the appear in the source-code?

Consider this example:

print(f"{(a:=1)}", f"{a=}",  f"{(a:=2)}", f"{a=}")  # print 1
print(f"{a=}")                                      # print 2

Is the first statement guaranteed to print "1 a=1 2 a=2"? And is the second guaranteed to print "a=2"?


Solution

  • Yes, python expressions are evaluated left to right. This is documented here.