Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function call as argument to print in python 3.x

a = 0
def f():
    global a
    a = 1
    print(a)
print(a, f(), a)

The output of the above code turns out to be the following:

1
0 None 1

Why is the function f called before printing the first argument a? Why is the value of the first argument 0, even after the function call?

like image 660
Sanjana S Avatar asked Nov 23 '25 11:11

Sanjana S


1 Answers

What you get as output is:

1
0 None 1

When you call print() it computes the elements to be printed first. As you have print(a) inside your f() what you get first is 1. Then, it starts printing. The value of a is 0 before you call the function. When you print a function that doesn't return anything, you get None. As you change the value globally, you get 1 at the end.

like image 90
T-800 Avatar answered Nov 24 '25 23:11

T-800