其他分享
首页 > 其他分享> > 多层装饰器、带参数装饰器

多层装饰器、带参数装饰器

作者:互联网

# 带参数的装饰器
# import time
# FLAGE = False           # 加个标志位,使全部的装饰器可以失效或有效
# def timmer_out(flag):
#     def timmer(func):
#         def inner(*args,**kwargs):
#             if flag:
#                 start =time.time()
#                 ret = func(*args,**kwargs)
#                 print(time.time()-start)
#                 return ret
#             else:
#                 ret = func(*args, **kwargs)
#                 return ret
#         return inner
#     return timmer
#
# @timmer_out(FLAGE)  # timmer_out(FLAGE)就是timmer
# def func():
#     time.sleep(0.1)
#     print('sleep:0.1')
# @timmer_out(FLAGE)
# def fun1():
#     time.sleep(0.5)
#     print('sleep:0.5')
# func()
# fun1()


# 多层装饰器
# 记录用户登陆情况,记录时间等场景
def fun1(func): def inner(*args,**kwargs): print(111) ret = func() print(222) return ret return inner def fun2(func): def inner1(*args,**kwargs): print('aaa') ret = func() print('bbb') return ret return inner1 @fun1 @fun2 def func(): print('冯') func()

 

标签:return,timmer,多层,参数,ret,func,print,装饰,def
来源: https://www.cnblogs.com/Mr-Feng/p/10803156.html