zoukankan      html  css  js  c++  java
  • Python函数式编程之闭包

    -------------------------函数式编程之*******闭包------------------------
    Note:

    一:简介
    函数式编程不是程序必须要的,但是对于简化程序有很重要的作用。
    Python中一切都是对象,函数也是对象
    a = 1
    a = 'str'
    a = func

    二:闭包
    闭包是由函数及其相关的引用环境组合而成的实体(即:闭包=函数+环境变量)
    如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,
    那么内部函数就被认为是闭包(closure),这个是最直白的解释!
    而且这个变量的值不会被模块中相同的变量值所修改!

    三:闭包的作用
    少使用全局变量,闭包可以避免使用全局变量
    可以实现在函数外部调用函数内部的值:
    print(f.__closure__[0].cell_contents)
    # 返回闭包中环境变量的值!
    模块操作是不能实现的!
    CODE:
      1 # ----------------------------------------------#
      2 # 闭包
      3 # ----------------------------------------------#
      4 # 函数内部定义函数
      5 
      6 
      7 def curve_pre():
      8     def curve():
      9         print("抛物线")
     10         pass
     11     return curve
     12 
     13 
     14 # 不能直接调用函数内部的函数
     15 # curve()
     16 func = curve_pre()
     17 func()
     18 
     19 
     20 def curve_pre1():
     21     a = 25  # 环境变量a的值在curve1外部
     22 
     23     def curve1(x):
     24         print("抛物线")
     25         return a * x ** 2
     26     return curve1       # 返回了的闭包
     27 
     28 
     29 f = curve_pre1()
     30 
     31 result = f(2)
     32 print(result)
     33 
     34 # 当在外部定义变量的时候,结果不会改变
     35 a = 10
     36 print(f(2))
     37 
     38 print(f.__closure__)    # 检测函数是不是闭包
     39 print(f.__closure__[0].cell_contents)    # 返回闭包中环境变量的值!
     40 
     41 # ----------------------------------------------#
     42 # 闭包的实例
     43 # ----------------------------------------------#
     44 
     45 
     46 def f1():
     47     m = 10
     48 
     49     def f2():
     50         m = 20  # 局部变量
     51         print("1:", m)  # m = 20
     52     print("2:", m)      # m = 10
     53     f2()
     54     print("3:", m)      # m = 10,臂包里面的值不会影响闭包外面的值
     55     return f2
     56 
     57 
     58 f1()
     59 f = f1()
     60 print(f.__closure__)    # 判断是不是闭包
     61 
     62 # ----------------------------------------------#
     63 # 闭包解决一个问题
     64 # ----------------------------------------------#
     65 # 在函数内部修改全局变量的值计算某人的累计步数
     66 # 普通方法实现
     67 sum_step = 0
     68 
     69 
     70 def calc_foot(step=0):
     71     global sum_step
     72     sum_step = sum_step + step
     73 
     74 
     75 while True:
     76     x_step = input('step_number:')
     77     if x_step == ' ':   # 输入空格结束输入
     78         print('total step is ', sum_step)
     79         break
     80     calc_foot(int(x_step))
     81     print(sum_step)
     82 
     83 # 闭包方式实现----->少使用全局变量,闭包可以避免
     84 
     85 
     86 def factory(pos):
     87 
     88     def move(step):
     89         nonlocal pos    # 修改外部作用域而非全局变量的值
     90         new_pose = pos + step
     91         pos = new_pose  # 保存修改后的值
     92         return pos
     93 
     94     return move
     95 
     96 
     97 tourist = factory(0)
     98 print(tourist(2))
     99 print(tourist(2))
    100 print(tourist(2))
    
    
    
     
  • 相关阅读:
    Xna小游戏开发【飞机空间大战】 碧血黄沙
    参考XNA官方Platformer模版,修改Platformer为横版可以滚动的小游戏 碧血黄沙
    云计算「半步巅峰」
    get_custom_field使用
    CCTM_FormElement 类
    php opendir说明
    js(chrome)打印对象函数
    operamasks.omItemSelector获取右边框ID方法
    {$vo.CreateDate|date="ymd H:i:s",###}
    添加xmlns:android="http://schemas.android.com/apk/res/android"的意思
  • 原文地址:https://www.cnblogs.com/faithyiyo/p/9765577.html
Copyright © 2011-2022 走看看