【发布时间】:2011-10-30 17:06:05
【问题描述】:
这是我需要的:
假设我有这个装饰器:
def deco(func):
def decoret(*args, **kwargs):
print(func.__locals__) # I know __locals__ is not valid, but I need something like this
return decoret
@deco
def func():
test1 = 123
test2 = 456
func()
我想获取 所有局部变量 的列表(就像我在函数内部调用 locals() 一样),所以我可以使用 test1 和 test2 访问 字典装饰器的 decoret 函数中的值。
我知道我可以通过使用 Python 检查模块来做到这一点,但我无法跟踪正确的帧来获取函数。
另外,我使用的是 Python 3.2 CPython。
【问题讨论】:
标签: python python-3.x decorator