函数嵌套,层级嵌套函数就是闭包,包就是一层的意思,闭就是封装的意思封装的变量


层级嵌套函数就是闭包,包就是一层的意思,闭就是封装的意思封装的变量


#
函数嵌套 def father(name): print('from father %s' %name)#函数值传递方式,参数,函数。。 def son(): print('from son') print(locals()) father('dog') def father(name): def son(): name='cat' print('我是%s'%name) son() father('dog')
#函数嵌套
def father(name):
    print('from father %s' %name)#函数值传递方式,参数,函数。。
    def son():
        print('from son')
    print(locals())
father('dog')
#层级嵌套函数就是闭包,包就是一层的意思,闭就是封装的意思封装的变量
def father(name):#3层函数

    def son():
        name='cat'
        print('我是%s'%name)#这里的name是cat
        def grandson():#最里层的name封装自己这就是闭包
            name='我自己'
            print('我爷爷是%s'%name)
        grandson()
    son()
father('dog')
原文地址:https://www.cnblogs.com/wfl9310/p/8991018.html