Function nesting refers to defining or calling another function in one function .
The following are simple examples to describe how to define and call another function in one function :
1. Define a function in a function
def outfun():
def infun1():
print(" This is an internally nested function 1")
def infun2():
print(" This is an internally nested function 2")
infun1()
infun2()
outfun()
stay python After running in the interpreter , The execution result is :
This is an internally nested function 1
This is an internally nested function 2
2. Call a function in a function
def test1():
print("*" * 20)
print("test1~~~")
def test2():
print("-" * 20)
print("test2~~~")
test1()
print("-" * 20)
test2()
stay python After running in the interpreter , The execution result is :
####################
test2~~~
1********************1
test1~~~
####################
call test1 Function time , First, complete the function test1 All tasks in .
Return call test2 Middle function test1 The location of , Then continue the execution of subsequent code .