Home >Backend Development >Python Tutorial >Detailed explanation of function nesting method using Python
Python language allows when defining a function, its function body contains the complete definition of another function. This is what we usually call nested definition.
Instance 1:
def OutFun(): #定义函数OutFun(), m=3 #定义变量m=3; def InFun(): #在OutFun内定义函数InFun() n=4 #定义局部变量n=4 print m+n #m相当于函数InFun()的全局变量 InFun() #OutFun()函数内调用函数InFun()
Instance 2:
def InFun(m): n=4 print m+n def OutFun() m=4 InFun(m)
Instance 2 first defines the function InFun(), and then defines the OutFun() function again. At this time, InFun( ) and OutFun() are two completely independent functions, and InFun() is called within the OutFun() function again; in fact, the nesting effect in instance 1 and instance 2 is the same, but in two different forms of expression.
The above is the detailed content of Detailed explanation of function nesting method using Python. For more information, please follow other related articles on the PHP Chinese website!