我有一個裝飾函數,我正在將其應用於類中的函數。裝飾者應該對_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