Beazley第100页提到:
>>>python.__closure__
(<cell at 0x67f50: str object at 0x69230>,)
>>>python.__closure__[0].cell_contents
Run Code Online (Sandbox Code Playgroud)
我的理解是这__closure__是一个列表但是这些单元格和str对象是什么?这看起来像一个单元组?
如果我有:
def f(x):
def g(y):
return x + y
return g
f2 = f(2)
Run Code Online (Sandbox Code Playgroud)
有没有办法找到f2将使用的'x'绑定?我看了检查,但无法判断是否有一些'框架'的东西适用.换句话说,我可以在下面定义一个closedVars():
def closed_vars(anF):
... return ...
assert closedVars(f2) == {'x': 2}
Run Code Online (Sandbox Code Playgroud)