3-10函数-嵌套函数

时间:2022-09-08 11:59:41
def func1():
    print('alex')
    
    def fun2():
        print('eric')


func1()  #运行结果:alex
def func1():
    print('alex')
    
    def func2():
        print('eric')
    func2()


func1()  #运行结果:alex
                  # eric

1.函数内部可以再次定义函数

2.函数要想执行需要被调用

age = 19
def func1():
    print('alex')
    age =78
    print('func1里面:',age)
    def func2():
        age = 84
        print('func2里面:',age)
    func2()

func1()

运行结果:

func1里面: 78
func2里面: 84

 

age = 19
def func1():
    print('alex')
    age =78
    print('func1里面:',age)
    def func2():
        print('func2里面:',age)
    func2()

func1()

运行结果:

func1里面: 78
func2里面: 78

age = 19
def func1():
   
  
    def func2():
        print('func2里面:',age)
    age = 78
    func2()

func1()

运行结果:func2里面: 78

age = 19
def func1():
   
  
    def func2():
        print('func2里面:',age)
    
    func2()
    age = 78

func1()

运行结果:

3-10函数-嵌套函数

 

age = 19
def func1():
   
    global age 
    def func2():
        print('func2里面:',age)
    
    func2()
    age = 78

func1()

运行结果:func2里面: 19

 

age = 19
def func1():
   
    global age 
    def func2():
        print('func2里面:',age)
    age = 78
    func2()
   

func1()
print('最外面:',age )

运行结果:

func2里面: 78
最外面: 78