详解python函数的闭包问题(内部函数与外部函数详述)

yipeiwu_com6年前Python基础

python函数的闭包问题(内嵌函数)

>>> def func1():
...   print ('func1 running...')
...   def func2():
...       print ('func2 running...')
...   func2()
... 
>>> func1()
func1 running...
func2 running...

内部函数func2作用域都在外部函数func1作用域之内
如果试图在外部函数的外部调用内部函数将会报错

>>> func2()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
NameError: name 'func2' is not defined

关于python的闭包问题

如果试图在一个内部函数里对外部作用域(不包括外部函数的外部作用域)的变量进行引用,内部函数就会被认为是闭包

>>> def FuncX(x):
...   def FuncY(y):
...       return x*y
...   return FuncY

对于FuncY函数来说,对在FuncX函数的整个作用域(FuncY函数的非全局作用域的外部作用)的变量x进行引用,自此就可以说FuncY函数就是所谓的闭包

>>> f = FuncX(8)
>>> f
<function FuncY at 0x7f3a436fc2a8>
>>> type(f)
<type 'function'>
>>> f(10)
80
>>> FuncX(7)(8)
56

由于闭包本身是基于内部函数这一概念而来,所以不能在外部函数的外部作用域对内部函数进行调用

>>> FuncY(8)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
NameError: name 'FuncY' is not defined

既然是基于内部函数这一概念而来,自然对于内部函数来说对引用外部函数作用域内的变量进行修改,将会启动解释器的屏蔽机制

>>> def Func1():
...   x = 233
...   def Func2():
...       x *=x
...       return x
...   return Func2()
... 
>>> Func1()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<stdin>", line 6, in Func1
 File "<stdin>", line 4, in Func2
UnboundLocalError: local variable 'x' referenced before assignment

x*=x的左值此时是内部函数作用域里的变量,此时试图将没有定义的数据进行平方操作,因此报错

>>> def Func1():
...   x = 233
...   def Func2():
...       x = 321
...       return x
...   return Func2()
... 
>>> Func1()
321

内部函数创建x变量并且屏蔽外部函数作用域内的x变量

python3之前的解决办法

应用容器类型(list,tuple之类的)存放外部函数作用域的变量从而不会被屏蔽机制屏蔽掉,因为容器类型不是存放在栈里面

>>> def Func1():
...   x = [233]
...   def Func2():
...       x[0] *= x[0]
...       return x[0]
...   return Func2()
... 
>>> Func1()
54289

python3之后的解决办法:nonlocal关键字

>>> def Func1():
...   x = 233
...   def Func2():
...   nonlocal x
...       x *= x
...       return x
...   return Func2()
... 
>>> Func1()
54289

以上所述是小编给大家介绍的python函数的闭包问题详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

Python判断一个三位数是否为水仙花数的示例

如下所示: daffodil = int(input('请输入一个三位数:')) if daffodil == pow(daffodil // 100 , 3) + pow(daff...

Python3字符串学习教程

字符串类型是python里面最常见的类型,是不可变类型,支持单引号、双引号、三引号,三引号是一对连续的单引号或者双引号,允许一个字符串跨多行。 字符串连接:前面提到的+操作符可用于字符串...

Python中装饰器高级用法详解

在Python中,装饰器一般用来修饰函数,实现公共功能,达到代码复用的目的。在函数定义前加上@xxxx,然后函数就注入了某些行为,很神奇!然而,这只是语法糖而已。 场景 假设,有一些工作...

DataFrame中去除指定列为空的行方法

一次,笔者在处理数据时想去除DataFrame中指定列的值为空的这一行,采用了如下做法,但是怎么都没有成功: # encoding: utf-8 import pandas as p...

python实现简单淘宝秒杀功能

本文实例为大家分享了Python淘宝秒杀的具体代码,供大家参考,具体内容如下 昨天茅台在线上搞秒杀,本来想着靠我惊人的手速去秒一瓶,结果。 所以痛定思痛,想想还是用脚本更靠谱。就在网上搜...