zoukankan      html  css  js  c++  java
  • 函数嵌套和nonlocal声明

    #函数的嵌套调用
    # def max(a,b):
    #     return a if a>b else b
    # def the_max(x,y,z): 
    #     c = max(x,y)
    #     return max(c,z)
    # print(the_max(1,2,3))
    #函数的嵌套定义
    #内部函数可以使用外部函数的变量
    # a = 1
    # def outer():
    #     a = 1
    #     def inner():
    #         a = 2
    #         def inner2():
    #             nonlocal a  #声明了一个上面第一层局部变量
    #             a += 1   #不可变数据类型的修改
    #         inner2()
    #         print('##a## : ', a)
    #     inner()
    #     print('**a** : ',a)
    # outer()
    # print('全局 :',a)

    关于nonlocal 声明

    # nonlocal 只能用于局部变量 找上层中离当前函数最近一层的局部变量
    # 声明了nonlocal的内部函数的变量修改会影响到 离当前函数最近一层的局部变量
    # 对全局无效
    # 对局部 也只是对 最近的 一层 有影响
    # a = 0
    # def outer():
    #     # a = 1
    #     def inner():
    #         # a = 2
    #         def inner2():
    #             nonlocal a
    #             print(a)
    #         inner2()
    #     inner()
    # outer()
  • 相关阅读:
    2021年4月27日 团队冲刺阶段01
    2021年4月26日
    2021年4月25日
    2021年4月24日
    2021年4月23日
    2021年4月22日
    2021年4月21日
    神奇的数列之“Last Defence ”
    经典圆交面积求解之“Intersection ”
    计蒜客第六场
  • 原文地址:https://www.cnblogs.com/aj-AJ/p/10817056.html
Copyright © 2011-2022 走看看