我有一个装饰函数,我正在将其应用于类中的函数。装饰者应该对_api
成员变量进行检查。但是,当我尝试执行此操作时,出现global name 'self' is not defined
错误。什么是正确的方法?在Python类中的装饰函数中访问自我
def requires_api(fn):
def wrapped(*args, **kwargs):
if self._api is not None:
return fn(*args, **kwargs)
else:
return None
return wrapped
@requires_api
def do_something(self):
...
kindall-有道理 - 谢谢 – Yarin 2012-04-08 16:26:58