我想为 Python 制作一个 @pure
装饰器,其中一部分是能够有选择地禁止访问函数的全局范围。
有没有办法以编程方式更改哪个字典事物用作函数的全局/外部范围?
所以例如在下面我希望能够拦截 h
中对 f
的访问并抛出错误,但我想允许访问 g
因为它是一个纯函数。
def f():
print("Non-pure function")
@pure
def g(i):
return i + 1
@pure
def h(i):
f()
return g(i)
最佳答案
您必须从旧函数对象创建一个新函数对象:
newfunc = type(h)(h.__code__, cleaned_globals, h.__name__, h.__defaults__, h.__closure__)
这里,cleaned_globals
是一个字典,用作新创建的函数对象的全局命名空间。所有其他参数都与原始函数相呼应。
cleaned_globals
当然可以基于 h.__globals__
的副本。
演示:
>>> def h(i):
... f()
... return g(i)
...
>>> def g(i):
... return i + 1
...
>>> def f():
... print("Non-pure function")
...
>>> h(1)
Non-pure function
2
>>> cleaned_globals = {'g': g}
>>> newfunc = type(h)(h.__code__, cleaned_globals, h.__name__, h.__defaults__, h.__closure__)
>>> newfunc(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in h
NameError: global name 'f' is not defined
>>> cleaned_globals['f'] = lambda: print('Injected function')
>>> newfunc(1)
Injected function
2
关于python - 更改用作函数全局范围的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18184357/