Python面试准备:利用装饰器装饰函数,改为需要用户名和密码登入才能使用函数

# 装饰器
def new_func(func):
    def wrappedfunc(username, password):
        if username == "root" and password == "123":
            print("Validation passed")
            return func()
        else:
            print("Wrong username or password")
            return 
    return wrappedfunc

@new_func
def run():
    print("Start execution")

run("root", "123")

输出为:
Validation passed
Start execution

run("root", "456")

输出为:
Wrong username or password

你可能感兴趣的:(Python,python,开发语言,面试)