Let's say I have
def foo(n):
print("foo",n)
def bar(n):
print("bar",n)
print("Hello",foo(1),bar(1))
I would expect the output to be:
Hello
foo 1 None
bar 1 None
But instead I get something which surprised me:
foo 1
bar 1
Hello None None
Why does Python call the functions first before printing the "Hello"? It seems like it would make more sense to print "Hello", then call foo(1)
, have it print its output, and then print "None" as it's return type. Then call bar(1)
and print that output, and print "None" as it's return type. Is there a reason Python (or maybe other languages) call the functions in this way instead of executing each argument in the order they appear?
Edit: Now, my followup question is what's happening internally with Python somehow temporarily storing return values of each argument if it's evaluating the expressions left to right? For example, now I understand it will evaluate each expression left to right, but the final line says Hello None None
, so is Python somehow remembering from the execution of each function that the second argument and third arguments have a return value of None
? For example, when evaluating foo()
, it will print foo 1
and then hit no return statement, so is it storing in memory that foo
didn't return a value?