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

问题描述 投票:1回答:1

在装饰者的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')
python python-decorators
1个回答
1
投票

您应该定义wrapper函数以将username作为参数:

def wrapper(username, *args, **kwargs):
    if username in registered_users:
        some_function(username, *args, **kwargs)
    else:
        print("User is not registed")
© www.soinside.com 2019 - 2024. All rights reserved.