vostro.beta
vostro.beta

Reputation: 303

why does "printing" occurs before "returning"?

My Code:

def f0():
    return 1

def f1():
    print("I don't deserve to be first :(")

print(f0(), f1())

Expected Output:

1 I don't deserve to be first :(
None

Actual Output:

I don't deserve to be first :(
1 None

I think it has to do something with the default behaviour of sys.__stdout__

Where exactly in Python Documention I can find details which are relevant to my question is what I am interested in.

Or may be someone to explain directly here (?)

Upvotes: 1

Views: 52

Answers (1)

kindall
kindall

Reputation: 184221

Has nothing to do with stdout, but rather with when values are printed. Your f0 is called first, but it doesn't print anything. Your f1 is called second, but it actually prints something.

The top-level print() call won't print anything until it has evaluated its arguments, which it does in argument order. So, f0 is called, printing nothing but returning 1, f1 is called and prints the message but returns None. print's argument values, i.e. the return values of the two functions, (1 and None), are then printed in order.

tl;dr: printing and returning are different things

Upvotes: 7

Related Questions