0

I found that Python's print function does not show values sequentially if some values are actually functions that also have print functions themselves, as in:

def f():
    print("function called")

print('hello',f()) 
# function called
# hello None

I'm really confused why the print function will go to f() first before printing 'hello'. My expected output is

hello function called
None

My thinking is print() will first prints 'hello', then it will execute f().

Could someone help with this? Thank you so much in advance!

  • How is the `print` function supposed to know that your print function also has a print statement in it? – Sayse Apr 12 '21 at 07:24
  • according to call stack, f() function will be resolved first then print function will be called finally so this output makes sense. – Anand Vidvat Apr 12 '21 at 07:24
  • This is *how all functions work*. Areguments are *fully evalauted* before being passed into the function. IOW, `f()` is *called first*, and the result is passed to `print`. `"hello"` *is evaluated first*, but that doesn't cause anything to be printed. – juanpa.arrivillaga Apr 12 '21 at 10:30
  • @juanpa.arrivillaga thank you so much! I overlooked the fact that `print` is also a function. Thanks so much for your help. – Probability is wonderful Apr 12 '21 at 23:42

1 Answers1

0

It is because the parameter f() is evaluated first, before the print() is executed.

ks2bmallik
  • 184
  • 3
  • 18