Python order in which functions are called in a print statement?
Let's say I have
def foo(n):
print("foo",n)
def bar(n):
print("bar",n)
print("Hello",foo(1),bar(1))
I expect the output to be as follows:
Hello
foo 1 None
bar 1 None
But instead I get what surprised me:
foo 1
bar 1
Hello None None
Why does Python call functions before printing "Hello"? It sounds like it makes sense to print "Hello", then call foo(1)
, print its output, and then print "None" as the return type. Then call bar(1)
and print this output and print "None" as the return type. Is there a reason why Python (or possibly other languages) call functions this way instead of executing each argument in the order they appear?
Edit . Now the question of what is going on internally with Python somehow temporarily preserves the return values ββof each argument if it evaluates expressions from left to right? For example, now I understand that it will evaluate each expression from left to right, but the final line states Hello None None
, so does Python somehow remember the execution of each function, that the second argument and the third argument have a return value None
? For example, when evaluated, foo()
it will print foo 1
and then not return a return statement, so it keeps in memory that it foo
did not return a value?
source to share
Quoting from the documentation :
Python evaluates expressions from left to right. Note that when evaluating an assignment, the right side is evaluated before the left side.
The bold accent is mine. So, all expressions are first evaluated and then passed to print
.
Notice the byte code for the print call:
1 0 LOAD_NAME 0 (print)
3 LOAD_CONST 0 ('Hello')
6 LOAD_NAME 1 (foo)
9 LOAD_CONST 1 (1)
12 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
15 LOAD_NAME 2 (bar)
18 LOAD_CONST 1 (1)
21 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
24 CALL_FUNCTION 3 (3 positional, 0 keyword pair)
27 RETURN_VALUE
foo
( LINE 12
) and bar
( LINE 21
) followed by print
( LINE 24
- 3 positional arguments).
As for the question of where these intermediate computed values ββare stored, that would be the call stack . print
accesses return values ββsimply by popping them off the stack.- Christian Dean
source to share
As the documentation states :
Python evaluates expressions from left to right . Note that when evaluating an assignment, the right side is evaluated before the left side.
This means that if you write:
print("Hello",foo(1),bar(1))
This is equivalent to:
arg1 = "Hello"
arg2 = foo(1)
arg3 = bar(1)
print(arg1,arg2,arg3)
So the arguments are evaluated before the function call .
This also happens when we have a tree, for example:
def foo(*x):
print(x)
return x
print(foo(foo('a'),foo('b')),foo(foo('c'),foo('d')))
Will print as:
>>> print(foo(foo('a'),foo('b')),foo(foo('c'),foo('d')))
('a',)
('b',)
(('a',), ('b',))
('c',)
('d',)
(('c',), ('d',))
(('a',), ('b',)) (('c',), ('d',))
Since Python evaluates arguments from left to right. It will foo(foo('a'),foo('b'))
rate foo(foo('a'),foo('b'))
first , but to rate it needs to rate first foo('a')
and then foo('b')
. Then he can do everything foo(foo('a'),foo('b'))
with the results of previous calls.
Then he wants to evaluate the second argument foo(foo('c'),foo('d'))
. But for this, he first evaluates foo('c')
and foo('d')
. Then he can evaluate foo(foo('c'),foo('d'))
and then, finally, he can estimate the final expression print(foo(foo('a'),foo('b')),foo(foo('c'),foo('d')))
.
So the score is equivalent to:
arg11 = foo('a') arg12 = foo('b') arg1 = foo(arg11,arg12) arg21 = foo('c') arg22 = foo('d') arg2 = foo(arg11,arg12) print(arg1,arg2)
source to share
The answer is simple: In python, type function arguments are print
always evaluated from left to right first.
Take a look at this stackoverflow question: In what order is the if statement evaluated in Python
And None
it's just the return value of the function. It executes the function first and then prints the return value
source to share
The enclosing function is not called until all of its arguments have been evaluated. This is consistent with the basic rules of mathematics, which states that operations in parentheses are performed before the outer ones. Since it print()
will always happen after foo()
and bar()
.
source to share