有没有办法访问用修饰符定义的Python函数的名称空间?
假设我使用缓存装饰器来定义一个新函数,如下所示:有没有办法访问用修饰符定义的Python函数的名称空间?,python,namespaces,python-decorators,Python,Namespaces,Python Decorators,假设我使用缓存装饰器来定义一个新函数,如下所示: def cached(funcy): cache = dict() def cache_funcy(x): if x in cache: return cache[x] else: print cache result = funcy(x) cache[x] = result re
def cached(funcy):
cache = dict()
def cache_funcy(x):
if x in cache:
return cache[x]
else:
print cache
result = funcy(x)
cache[x] = result
return result
return cache_funcy
@cached
def triple(x):
return 3*x
调用函数triple
四次会产生以下输出:
>>> triple(1)
{}
3
>>> triple(2)
{1: 3}
6
>>> triple(2)
6
>>> triple(4)
{1: 3, 2: 6}
12
我的理解是,函数triple
可以访问本地称为cache
的字典,因为该字典存在于定义triple
的命名空间中。在外部全局范围内无法直接访问此词典
是否可以通过函数triple
的某种属性访问此缓存
字典
注意:我想知道,如果不通过cache\u funcy.cache=cache
之类的东西显式地将cache
设为triple
的属性,是否有可能做到这一点,在cached
的定义中,实际上,这个dict并没有存储在函数的本地名称空间中,它是一个自由变量,因此,它存储在函数闭包中在Python 2中,请考虑:
In [1]: def cached(funcy):
...: cache = dict()
...: def cache_funcy(x):
...: if x in cache:
...: return cache[x]
...: else:
...: print cache
...: result = funcy(x)
...: cache[x] = result
...: return result
...: return cache_funcy
...:
...: @cached
...: def triple(x):
...: return 3*x
...:
In [2]: triple(1)
{}
Out[2]: 3
In [3]: triple(2)
{1: 3}
Out[3]: 6
现在:
In [5]: triple.func_closure
Out[5]:
(<cell at 0x10e4e7be8: dict object at 0x10e7ec910>,
<cell at 0x10e7b2590: function object at 0x10e81ede8>)
请注意,Python 3中函数的属性略有不同,这里有一个直接属性\uuuuu closure\uuuu
,因此:
In [4]: triple.__closure__
Out[4]:
(<cell at 0x1026dbc78: dict object at 0x1028d1bd0>,
<cell at 0x1026dbf48: function object at 0x1028e59d8>)
[4]中的:三重闭包__
出[4]:
(,
)
实际上,在Python2中,自从Python2.6以来,这些下划线属性是为了向前兼容而添加的,所以除非您使用的是低于Python2.6的版本,否则该属性也存在
因此,出于兼容性原因,您可能应该使用\uuuuu闭包\uuuuu
In [4]: triple.__closure__
Out[4]:
(<cell at 0x1026dbc78: dict object at 0x1028d1bd0>,
<cell at 0x1026dbf48: function object at 0x1028e59d8>)