在装饰器的require_registered
包装器中,如何检查“ John”是否在注册用户中?
def require_registered(some_function):
def wrapper(*args, **kwargs):
if username_parameter_from_login_function in registered_users: #how to replace "username_parameter_from_login_function"?
some_function(*args, **kwargs)
else:
print("User is not registed")
return wrapper
@require_registered
def login(username):
print(username + ' has been logged in')
login('John')
答案 0 :(得分:1)
您还应该定义wrapper
函数以将username
作为参数:
def wrapper(username, *args, **kwargs):
if username in registered_users:
some_function(username, *args, **kwargs)
else:
print("User is not registed")