def require(role):
def wrapper(fn):
def new_fn(*args, **kwargs):
if not role in kwargs.get('roles', []):
print("%s not in %s" % (role, kwargs.get('roles', [])))
raise Exception("Unauthorized")
return fn(*args, **kwargs)
return new_fn
return wrapper
@require('admin')
def get_users(**kwargs):
return ('Alice', 'Bob')
上面的代碼參數化裝飾器require
與admin
。看起來功能get_users
傳遞給wrapper
的參數fn
。但是,get_users
如何傳遞給參數fn
?參數如何通過python的裝飾器傳遞?
你的意思是你想給'get_users()'訪問'role'的值? –