有没有办法修改闭包内的其中一个变量的绑定值?看一下这个例子来更好地理解它.
def foo():
var_a = 2
var_b = 3
def _closure(x):
return var_a + var_b + x
return _closure
localClosure = foo()
# Local closure is now "return 2 + 3 + x"
a = localClosure(1) # 2 + 3 + 1 == 6
# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0
# ...but what magic? Is this even possible?
# Local closure is now "return 0 + 3 + x"
b = localClosure(1) # …Run Code Online (Sandbox Code Playgroud) 在以下示例中:
def speak(volume):
def whisper(text):
print(text.lower() + ('.' * volume))
def yell(text):
print (text.upper() + ('!' * volume))
if volume > 1:
return yell
elif volume <= 1:
return whisper
func = speak(volume=10)
func('hello')
HELLO!!!!!!!!!! # <== obviously `10` is stored in `func` somewhere
Run Code Online (Sandbox Code Playgroud)
给定func,我将如何获得“体积”?func命名空间中是否有东西可以赋予值10?我以为也许会在其中func.__globals__,func.__dict__但两者都不会。
我有一个在此函数中创建的lambda对象:
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
self.record(lambda s:
s.add_url_rule(rule, endpoint, view_func, **options))
Run Code Online (Sandbox Code Playgroud)
使用func_closurelambda函数对象,我可以访问lambda函数的闭包范围:
(<cell at 0x3eb89f0: str object at 0x2fb4378>,
<cell at 0x3eb8a28: function object at 0x3cb3a28>,
<cell at 0x3eb8a60: str object at 0x3ebd090>,
<cell at 0x3eb8b08: dict object at 0x3016ec0>)
Run Code Online (Sandbox Code Playgroud)
仔细观察(cell_contents每个cell对象的属性)向我展示:
>>> [c.cell_contents for c in func.func_closure]
['categoryDisplay',
<function indico.web.flask.util.RHCategoryDisplay>,
'/<categId>/',
{}]
Run Code Online (Sandbox Code Playgroud)
lambda函数是由这个调用创建的:
add_url_rule('/<categId>/', 'categoryDisplay', rh_as_view(RHCategoryDisplay))
Run Code Online (Sandbox Code Playgroud)
如您所见,顺序与函数的参数顺序或lambda中使用参数的顺序不匹配.虽然我可以根据其类型/内容轻松找出哪个元素是什么,但我想以更清洁的方式进行.
所以我的问题是:如何将它与原始变量名称相关联(或者至少在函数参数的情况下位置)?
如果我有一个包含一些非局部变量(在闭包中)的函数,我如何访问该变量?我可以修改它吗?如果可以,如何修改?这是此类函数的示例:
def outer():
x = 1
def inner(y):
nonlocal x
return x + y
return inner
inner = outer()
# how do I get / change the value of x inside inner?
Run Code Online (Sandbox Code Playgroud)
(如果这个问题已经在其他地方得到了回答,我深表歉意;我找不到它,所以我想一旦解决了我就会分享答案)