更改将什么字典用作函数的全局范围 [英] Change what dictionary serves as a function's global scope

查看:65
本文介绍了更改将什么字典用作函数的全局范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Python创建一个 @pure 装饰器,其中一部分是能够有选择地禁止访问该函数的全局范围。

I want to make an @pure decorator for Python, part of this is being able to selectively disallow access to the global scope of the function.

有没有办法以编程方式更改哪个字典事物充当函数的全局/外部作用域?

Is there a way to programmatically change which dictionary thing serves as a function's global/external scope?

例如,在以下示例中我希望能够在 h 中拦截对 f 的访问并抛出错误,但是我想允许访问到 g ,因为它是一个纯函数。

So for instance in the following I want to be able to intercept the access to f in h and throw an error, but I want to allow access to g because it's a pure function.

def f():
    print("Non-pure function")

@pure
def g(i):
    return i + 1

@pure
def h(i):
    f()
    return g(i)


推荐答案

您必须从旧版本中创建一个 new 函数对象:

You would have to create a new function object from the old one:

newfunc = type(h)(h.__code__, cleaned_globals, h.__name__, h.__defaults__, h.__closure__)

此处, c leaned_globals 是一个字典,将用作新创建的函数对象的全局命名空间。所有其他参数都回显原始函数。

Here, cleaned_globals is a dictionary that is to be used as the global namespace for the newly created function object. All other arguments echo the original function's.

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

这篇关于更改将什么字典用作函数的全局范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆