Python记录11:叠加多个装饰器+有参装饰器

Posted 同济小孙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python记录11:叠加多个装饰器+有参装饰器相关的知识,希望对你有一定的参考价值。


# import time
#
# def timmer(func): #func=最原始的index的内存地址
# def wrapper(*args,**kwargs):
# start=time.time()
# res=func(*args,**kwargs)
# stop=time.time()
# print(stop - start)
# return res
# return wrapper
#
# @timmer #index=timmer(index) #index=wrapper的内存地址
# def index():
# print(‘index function‘)
# time.sleep(1)
#
# index(1,2,3,) #wrapper(1,2,3)





# 叠加多个装饰器
#1. 加载顺序: 自下而上
#2. 执行顺序


# import time
#
# def deco1(func1): #func1=wrapper2的内存地址
# # print(‘deco1运行‘)
# def wrapper1(*args,**kwargs):
# print(‘wrapper1‘)
# res=func1(*args,**kwargs)
# return res
# return wrapper1
#
# def deco2(func2): #func2=wrapper3的内存地址
# # print(‘deco2运行‘)
# def wrapper2(*args,**kwargs):
# print(‘wrapper2‘)
# res=func2(*args,**kwargs)
# return res
# return wrapper2
#
# def deco3(func3): #func3=最原始那个index的内存地址
# # print(‘deco3运行‘)
# def wrapper3(*args,**kwargs):
# print(‘wrapper3‘)
# res=func3(*args,**kwargs)
# return res
# return wrapper3
#
#
# # index=wrapper1
# @deco1 # index=deco1(wrapper2)
# @deco2 # wrapper2=deco2(wrapper3)
# @deco3 # wrapper3=deco3(最原始那个index的内存地址)
# def index():
# print(‘index function‘)
# time.sleep(1)
#
# index() #wrapper1()


import time
current_user={‘username‘:None}

def timmer(func): #func=最原始的index的内存地址
def wrapper1(*args,**kwargs):
start=time.time()
res=func(*args,**kwargs) #================>wrapper2
stop=time.time()
print(stop - start)
return res
return wrapper1

def auth(func):
def wrapper2(*args,**kwargs):
if current_user[‘username‘]:
res = func(*args, **kwargs)
return res
name=input(‘username>>: ‘).strip()
pwd=input(‘password>>: ‘).strip()
if name == ‘egon‘ and pwd == ‘123‘:
current_user[‘username‘]=name
res=func(*args,**kwargs)
return res
else:
print(‘账号密码错误...‘)
return wrapper2


@timmer
@auth
def index():
print(‘index function‘)
time.sleep(1)


index()




import time
current_user={‘username‘:None}

def outter(engine=‘file‘):
def auth(func):
def wrapper2(*args,**kwargs):
# if current_user[‘username‘]:
# res = func(*args, **kwargs)
# return res
name=input(‘username>>: ‘).strip()
pwd=input(‘password>>: ‘).strip()
if engine == ‘file‘:
print(‘基于文件的认证‘)
if name == ‘egon‘ and pwd == ‘123‘:
current_user[‘username‘]=name
res=func(*args,**kwargs)
return res
else:
print(‘账号密码错误...‘)
elif engine == mysql‘:
print(‘基于mysql的认证‘)
elif engine == ‘ldap‘:
print(‘基于ldap的认证‘)
else:
print("不合法的认证源")
return wrapper2
return auth

@outter(engine=‘file‘) # 账号密码是来自于文件 index=aaa.txt(index) #index=wrapper2
def index():
print(‘index function‘)
time.sleep(1)

@outter(engine=‘mysql‘) # 账号密码是来自于mysql # home=bbb(home) #home=wrapper2
def home(name):
print(‘home function‘,name)
time.sleep(1)

index() #wrapper2()
home(‘egon‘) #wrapper2(‘egon‘)
 

以上是关于Python记录11:叠加多个装饰器+有参装饰器的主要内容,如果未能解决你的问题,请参考以下文章

装饰器的补充(叠加多个装饰器,有参装饰器,三元表达式,生成式,匿名函数)

24Python之有参装饰器

python中的无参装饰器和有参装饰器

Python:装饰器之有参装饰器

Python-23_装饰器-04_练习---无参装饰器有参装饰器

15 装饰器 开闭原则 代参装饰器 多个装饰器同一函数应用