什么是包含在obj .__闭包内? [英] What exactly is contained within a obj.__closure__?
问题描述
Beazley第100页提及:
>>> python .__ closure__
(< cell at 0x67f50:str对象在0x69230>,)
>>> python .__ closure __ [0] .cell_contents
b $ b
我的理解是 __ closure __
是一个列表,但是这是所有这些
单元格的东西和str对象?这看起来像一个1元的元组?
闭包单元格指的是函数所需的值, 。
当Python编译一个嵌套函数时,它会注意它引用的任何变量,但只在嵌套的代码对象的父函数函数和父作用域。这些是 __ code __
对象上的 co_freevars
和 co_cellvars
属性
函数闭包持有一个单元格的元组,每个自由变量各一个(在 co_freevars
中命名);单元格是对父作用域的局部变量的特殊引用,它们遵循这些局部变量所指向的值。最好用一个例子来说明:
def foo():
def bar():
print(spam)
spam ='ham'
bar()
spam ='eggs'
bar()
返回bar
b = foo()
b()
在上面的例子中, bar
有一个闭包单元格,指向 foo $ c>中的
。 spam
$ c>。单元格遵循 spam
的值。更重要的是,一旦 foo()
完成并返回 bar
,单元格将继续引用值即使 spam
里面的 foo
不再存在,code> egg
因此,上述代码输出:
> ;> b = foo()
ham
egg
>>> b()
egg
和 b .__ closure __ [0] .cell_contents
是'eggs'
。
请注意, > bar()
时调用;闭包不捕获这里的值。当你生成引用循环变量的嵌套函数(使用 lambda
表达式或 def
语句)时, / p>
def foo():
bar = []
','salad'):
bar.append(lambda:spam)
返回bar
for foo():
打印栏$ b
上面将会连续三次打印 salad
,因为所有三个 lambda
函数引用 spam
变量,而不是创建函数对象时绑定的值。当 for
循环结束时, spam
绑定到'salad'
,所以三个闭包将解析为该值。
Beazley pg 100 mentions:
>>>python.__closure__
(<cell at 0x67f50: str object at 0x69230>,)
>>>python.__closure__[0].cell_contents
my understanding is that __closure__
is a list but what's all this
cell stuff and str object?? That looks like a 1-ary tuple?
Closure cells refer to values needed by the function but are taken from the surrounding scope.
When Python compiles a nested function, it notes any variables that it references but are only defined in a parent function (not globals) in the code objects for both the nested function and the parent scope. These are the co_freevars
and co_cellvars
attributes on the __code__
objects of these functions, respectively.
Then, when you actually create the nested function (which happens when the parent function is executed), those references are then used to attach a closure to the nested function.
A function closure holds a tuple of cells, one each for each free variable (named in co_freevars
); cells are special references to local variables of a parent scope, that follow the values those local variables point to. This is best illustrated with an example:
def foo():
def bar():
print(spam)
spam = 'ham'
bar()
spam = 'eggs'
bar()
return bar
b = foo()
b()
In the above example, the function bar
has one closure cell, which points to spam
in the function foo
. The cell follows the value of spam
. More importantly, once foo()
completes and bar
is returned, the cell continues to reference the value (the string eggs
) even though the variable spam
inside foo
no longer exists.
Thus, the above code outputs:
>>> b=foo()
ham
eggs
>>> b()
eggs
and b.__closure__[0].cell_contents
is 'eggs'
.
Note that the closure is dereferenced when bar()
is called; the closure doesn't capture the value here. That makes a difference when you produce nested functions (with lambda
expressions or def
statements) that reference the loop variable:
def foo():
bar = []
for spam in ('ham', 'eggs', 'salad'):
bar.append(lambda: spam)
return bar
for bar in foo():
print bar()
The above will print salad
three times in a row, because all three lambda
functions reference the spam
variable, not the value it was bound to when the function object was created. By the time the for
loop finishes, spam
was bound to 'salad'
, so all three closures will resolve to that value.
这篇关于什么是包含在obj .__闭包内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!