我正在尝试从装饰器中获取局部变量。一个例子:
def needs_privilege(privilege, project=None):
"""Check whether the logged-in user is authorised based on the
given privilege
@type privilege: Privilege object, id, or str
@param privilege: The requested privilege"""
def validate(func, self, *args, **kwargs):
"""Validator of needs_privillige"""
try: check(self.user, privilege, project)
except AccessDenied:
return abort(status_code=401)
else:
return func(self, *args, **kwargs)
return decorator(validate)
装饰一个函数后,像这样:
@needs_privilege("some_privilege")
def some_function():
pass
我想从 some_function 中检索“特权”变量(validate() 使用)。搜索了一个多小时后,我感到很失落。这可能吗?
编辑:让我更彻底地描述一下我的问题:我可以在不执行 some_function 的情况下获得字符串“some_prvilege”吗?就像是:
a = getattr(module, 'somefunction')
print a.decorator_arguments
? 感谢您到目前为止对我的帮助!