gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:05:45 25 4
gpt4 key购买 nike

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

最佳答案

您应该定义您的包装器函数以将用户名作为参数:

def wrapper(username, *args, **kwargs):
if username in registered_users:
some_function(username, *args, **kwargs)
else:
print("User is not registed")

关于python - 如何评估从装饰器包装器中调用的函数的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com