如何评估从装饰器的包装器中调用的函数的参数?

时间:2018-08-29 05:34:48

标签: python python-decorators

在装饰器的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')

1 个答案:

答案 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")