def f(x):
def g():
x='abc'
print('x=',x)
def h():
z=x
print('z=',z)
x=x+1
print('x=',x)
h()
g()
print('x=',x)
return g
x=3
z=f(x)
print('x=',x)
print('z=',z)
z()
Output
x= 4
z= 4
x= abc
x= 4
x= 3
z= <function f.<locals>.g at 0x000000F6919EF9D8>
x= abc
I don't understand how we call z() as a function when it is a variable. Also, I don't understand how we got the last 2 lines of output.
f(x)returnsgwhich is a function, hence callingz()will callg().f()?zis holding only the returned value from functionf(x). That returned value happened to be a function. So callingz()calls that function and that function includes only one print statement.