TypeError:装饰器中无法调用“ NoneType”对象

时间:2020-06-03 17:25:52

标签: python python-decorators

#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)

1 个答案:

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