其中 X 是任何支持某种闭包风格的编程语言(C#、Javascript、Lisp、Perl、Ruby、Scheme 等)。
在 Python中的闭包中提到了一些限制(与 Ruby 的闭包相比),但这篇文章已经过时,现代 Python 中不再存在许多限制。
查看具体限制的代码示例会很棒。
相关问题:
其中 X 是任何支持某种闭包风格的编程语言(C#、Javascript、Lisp、Perl、Ruby、Scheme 等)。
在 Python中的闭包中提到了一些限制(与 Ruby 的闭包相比),但这篇文章已经过时,现代 Python 中不再存在许多限制。
查看具体限制的代码示例会很棒。
相关问题:
目前,最重要的限制是您不能分配给外部范围变量。换句话说,闭包是只读的:
>>> def outer(x):
... def inner_reads():
... # Will return outer's 'x'.
... return x
... def inner_writes(y):
... # Will assign to a local 'x', not the outer 'x'
... x = y
... def inner_error(y):
... # Will produce an error: 'x' is local because of the assignment,
... # but we use it before it is assigned to.
... tmp = x
... x = y
... return tmp
... return inner_reads, inner_writes, inner_error
...
>>> inner_reads, inner_writes, inner_error = outer(5)
>>> inner_reads()
5
>>> inner_writes(10)
>>> inner_reads()
5
>>> inner_error(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 11, in inner_error
UnboundLocalError: local variable 'x' referenced before assignment
除非另有声明,否则在本地范围(函数)中分配的名称始终是本地的。虽然有一个“全局”声明来声明一个全局变量,即使它被分配给它,但对于封闭的变量,还没有这样的声明。在 Python 3.0 中,有(将有)'nonlocal' 声明可以做到这一点。
同时,您可以通过使用可变容器类型来解决此限制:
>>> def outer(x):
... x = [x]
... def inner_reads():
... # Will return outer's x's first (and only) element.
... return x[0]
... def inner_writes(y):
... # Will look up outer's x, then mutate it.
... x[0] = y
... def inner_error(y):
... # Will now work, because 'x' is not assigned to, just referenced.
... tmp = x[0]
... x[0] = y
... return tmp
... return inner_reads, inner_writes, inner_error
...
>>> inner_reads, inner_writes, inner_error = outer(5)
>>> inner_reads()
5
>>> inner_writes(10)
>>> inner_reads()
10
>>> inner_error(15)
10
>>> inner_reads()
15
我看到人们在使用 Python 时遇到的唯一困难是当他们尝试将变量重新分配等非功能性特性与闭包混合使用时,当这不起作用时会感到惊讶:
def outer ():
x = 1
def inner ():
print x
x = 2
return inner
outer () ()
通常只是指出一个函数有它自己的局部变量就足以阻止这种愚蠢。
与 Javascript 闭包相比,Python 闭包的一个限制(或“限制”)是它不能用于有效的数据隐藏
var mksecretmaker = function(){
var secrets = [];
var mksecret = function() {
secrets.push(Math.random())
}
return mksecret
}
var secretmaker = mksecretmaker();
secretmaker(); secretmaker()
// privately generated secret number list
// is practically inaccessible
import random
def mksecretmaker():
secrets = []
def mksecret():
secrets.append(random.random())
return mksecret
secretmaker = mksecretmaker()
secretmaker(); secretmaker()
# "secrets" are easily accessible,
# it's difficult to hide something in Python:
secretmaker.__closure__[0].cell_contents # -> e.g. [0.680752847190161, 0.9068475951742101]
nonlocal
通过以下语句在 Python 3 中修复:
该
nonlocal
语句导致列出的标识符引用最近的封闭范围内的先前绑定的变量,不包括全局变量。这很重要,因为绑定的默认行为是首先搜索本地命名空间。该语句允许封装代码重新绑定全局(模块)范围之外的局部范围之外的变量。
@约翰米利金
def outer():
x = 1 # local to `outer()`
def inner():
x = 2 # local to `inner()`
print(x)
x = 3
return x
def inner2():
nonlocal x
print(x) # local to `outer()`
x = 4 # change `x`, it is not local to `inner2()`
return x
x = 5 # local to `outer()`
return (inner, inner2)
for inner in outer():
print(inner())
# -> 2 3 5 4
评论@Kevin Little 的答案以包含代码示例
nonlocal
在 python3.0 上没有完全解决这个问题:
x = 0 # global x
def outer():
x = 1 # local to `outer`
def inner():
global x
x = 2 # change global
print(x)
x = 3 # change global
return x
def inner2():
## nonlocal x # can't use `nonlocal` here
print(x) # prints global
## x = 4 # can't change `x` here
return x
x = 5
return (inner, inner2)
for inner in outer():
print(inner())
# -> 2 3 3 3
另一方面:
x = 0
def outer():
x = 1 # local to `outer`
def inner():
## global x
x = 2
print(x) # local to `inner`
x = 3
return x
def inner2():
nonlocal x
print(x)
x = 4 # local to `outer`
return x
x = 5
return (inner, inner2)
for inner in outer():
print(inner())
# -> 2 3 5 4
它适用于python3.1-3.3
在 3.0 之前,更好的解决方法是将变量作为默认参数包含在封闭的函数定义中:
定义 f() x = 5 定义 g(y, z, x=x): x = x + 1