python的有参装饰器
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))
相关推荐
夜斗不是神 2020-11-17
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16