#decorators in python
users={"ravneet":"elitebook","singh":"punjab"}
def login_required(func):
def wrapper(username, password, *args, **kwargs):
if username in users and users[username]==password:
func(*args,**kwargs)
else:
print("not authenticated")
return wrapper
@login_required
def add(a,b):
print("a+b: ",a+b)
add("ravneet","elitebook",2,3)
答案 0 :(得分:0)
您的return wrapper
有一个额外的缩进,使login_required()
返回None
。在下面固定(和repl.it link):
def login_required(func):
def wrapper(username, password, *args, **kwargs):
if username in users and users[username]==password:
func(*args,**kwargs)
else:
print("not authenticated")
return wrapper
@login_required
def add(a,b):
print("a+b: ",a+b)
add("ravneet","elitebook",2,3)