python的有参装饰器

wklken的笔记 2020-05-27

def auth(db_type):    def deco(func):        def wrapper(*args, **kwargs):            name = input(‘your name>>>:‘.strip())            pwd = input(‘your password>>>:‘.strip())            if db_type == ‘file‘:                print(‘基于文件的验证‘)                # 从文件中取账号密码进行验证                if name == ‘engo‘ and pwd == ‘123‘:                    print(‘login successful‘)                    res = func(*args, **kwargs)                    return res                else:                    print(‘user or password error‘)            elif db_type == ‘mysql‘:                print(‘基于mysql的验证‘)            elif db_type == ‘ldap‘:                print(‘基于ldap的验证‘)            else:                print(‘不支持改db_type‘)        return wrapper    return deco# deco = auth(db_type=‘file‘)@auth(db_type=‘file‘)  # 碰到函数+(),先把@放一边,首先执行函数调用,然后结果是@函数返回值(@deco)# @deco会把index作为参数传入去调用函数d--eco(index)def index(x, y):    print(‘index->>%s:%s‘ %(x, y))

python的有参装饰器

 python的有参装饰器

 

 

相关推荐