zoukankan      html  css  js  c++  java
  • 函数嵌套,层级嵌套函数就是闭包,包就是一层的意思,闭就是封装的意思封装的变量

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


    #
    函数嵌套 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')
  • 相关阅读:
    2-SAT·hihoCoder音乐节
    Music in Car
    Game with a Strip
    Oleg and Little Ponies
    组合数性质求K个数选取i*j个数分成j组的方案数
    Python学习笔记03
    Python学习笔记02
    Python 学习笔记01
    欺骗侦测
    Oracle 使用小计(4)
  • 原文地址:https://www.cnblogs.com/wfl9310/p/8991018.html
Copyright © 2011-2022 走看看