rb612
rb612

Reputation: 5563

Python order in which functions in print statement are called?

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?

Upvotes: 5

Views: 988

Answers (4)

cs95
cs95

Reputation: 402483

Quoting from the documentation:

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

Bold emphasis mine. So, all expressions are first evaluated and then passed to print.

Observe 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) are first called, followed by print (LINE 24 - 3 positional args).

As to the question of where these intermediate computed values are stored, that would be the call stack. print accesses the return values simply by poping them off of the stack. - Christian Dean

Upvotes: 3

Liam
Liam

Reputation: 6439

The answer is simple: In python the arguments of a function like print are always first evaluated left to right.

Take a look at this stackoverflow question: In which order is an if statement evaluated in Python

And None is just the return value of the function. It executes the function first and then print its return value

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

As is specified in the documentation:

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

This thus means that if you write:

print("Hello",foo(1),bar(1))

It 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 for instance have a tree:

def foo(*x):
    print(x)
    return x

print(foo(foo('a'),foo('b')),foo(foo('c'),foo('d')))

This prints 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 thus evaluates arguments left-to-right. It will first evaluate foo(foo('a'),foo('b')), but in order to evaluate foo(foo('a'),foo('b')), it first needs to evaluate foo('a'), followed by foo('b'). Then it can all foo(foo('a'),foo('b')) with the results of the previous calls.

Then it wants to evaluate the second argument foo(foo('c'),foo('d')). But in order to do this, it thus first evaluates foo('c') and foo('d'). Next it can evaluate foo(foo('c'),foo('d')), and then finally it can evaluate the final expression: print(foo(foo('a'),foo('b')),foo(foo('c'),foo('d'))).

So the evaluation 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)

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798636

The enclosing function is not called until all of its arguments have been evaluated. This is consistent with the basic rules of mathematics that state that operations within parentheses are performed before those outside. As such print() will always happen after both foo() and bar().

Upvotes: 2

Related Questions