鱼C论坛

 找回密码
 立即注册
查看: 2183|回复: 1

[技术交流] python 020 内嵌函数和闭包

[复制链接]
发表于 2018-6-20 14:11:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 风不会停息 于 2018-6-20 22:33 编辑

1. 如果想在函数内部定义全局变量, 用 global 关键字

  1. >>> count = 5
  2. >>> def MyFun():
  3.                 global count
  4.                 count = 10
  5.                 print(count)

  6. >>> MyFun()
  7. 10
  8. >>> count
  9. 10
复制代码


2. 对于函数嵌套, 外层函数对于内层函数来说时透明的, 而内层函数对于外层函数是不可见的, 不能在外层函数访问到内层函数的变量, 有一个特例, 当在外层函数和内层函数中定义了同一个变量时, 外层函数的变量会被暂时 “屏蔽” 掉, 例如:

  1. def outside():
  2.     var = 5
  3.     def inside():
  4.         print(var)
  5.         var = 3
  6.         
  7.     inside()
  8. outside()
复制代码


此时程序会报错

3. 访问内层函数的两种办法:

  1. def funOut():
  2.     def funIn():
  3.         print('宾果!你成功访问到我啦!')
  4.     return funIn

  5. funOut()()
  6. 宾果!你成功访问到我啦!
复制代码

  1. def funOut():
  2.     def funIn():
  3.         print('宾果!你成功访问到我啦!')
  4.     return funIn()

  5. funOut()
  6. 宾果!你成功访问到我啦!
复制代码


4. 在内层函数中如果想修改外层函数的值, 使用  nonlocal 关键字:

  1. >>> def Fun1():
  2.                 x = 5
  3.                 def Fun2():
  4.                         nonlocal x
  5.                         x *= x
  6.                         return x
  7.                 return Fun2()

  8. >>> Fun1()
  9. 25
复制代码


5. 闭包: 在一个外函数中定义了一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用。这样就构成了一个闭包。 例如

  1. >>> def  fun1(a):
  2.         b = 10
  3.         def  fun2():
  4.                 print(a + b)
  5.         return fun2()
复制代码


python中一切皆对象, 形成闭包后, 内层函数引用的外层函数定义的变量叫做闭包变量,闭包变量不会释放,会像全局变量一样, 会随着闭包函数的调用而更新



最后附一个 020 动动手第1题程序:

  1. str_long = """待输入的字符串"""

  2. length = len(str_long)

  3. for each in range(length):
  4.    
  5.     if  str_long[each] == '\n':
  6.             continue                        #检测换行符
  7.         
  8.     count_left = 0                       #检测左边大写字母
  9.     count_right = 0                    #检测右边大写字母
  10.     if  each >= 2:                      #防止分片时发生重复
  11.         i = each
  12.         if  str_long[each].islower():       #小写字母
  13.             str1 =  str_long[ : (each)]
  14.             str2 =  str_long[(each + 1) : ]             #检查到小写字母时, 将小写字母左右分片
  15.             while   i  != 0 and str1[i - 1].isupper():     #str1[i] 会越右界, 所以要用str[i - 1], i = 0 时 (i - 1) 到达左边界
  16.                 count_left += 1
  17.                 i -= 1
  18.             i = 0           #str2从0开始计数
  19.             while  ( i != len(str2) ) and str2[i].isupper():          #i = len(str2) 到达右边界
  20.                 count_right += 1
  21.                 i += 1
  22.     if  ( count_left == 3 ) and ( count_right == 3 ):           
  23.             print(str_long[each], end = '')                       #打印符合条件的小写字母
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-28 14:56:28 | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 22:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表