双层语法糖
三层语法糖(多层)
装饰器的修复技术(了解)
有参装饰器*
装饰器的写法:自上而下
执行:自下而上
双层:
@login_auth
@outer
执行顺先 先执行outer——被装饰名字作为参数传入outer里,get_time=outer(index)。
通过调用返回值(前提是如果有双层及以上装饰器,不可用index)
@login_auth调用 login_auth() 将下方被装饰器对象 当作对象 传送来,又因为上方没有装饰器所以index=login_auth (get_time) 而且login_auth的返回值是auth 所以 index()=auth()
当目标是最后一个装饰器时,用最原始被装饰对象来作 变量值。
通过执行outer函数——outer(func) 此时func是index,返回值get_time 到get_time=outer(index)
再执行login_auth函数 并调用login_auth(func)返回值auth 此时func是 get_time ,再返回到 index=login_auth(get_time),《此时位于最后一个装饰器 所以用index》 然后回到index() 此时等于auth() 调用auth() 并执行 到 func() 此时为get_time。
所以func()=get_time() 而因get_time所以回到开头def outer(func), 再次执行get_time 直到res = func(*args,**kwargs) 此时func为 index——func()=index() 又因为index() =auth() 到最后执行
def index():
time.sleep(3)
print('from index')
多层:
装饰器的修复技术
有参装饰器*
标签:index,outer,15days,get,Python,auth,func,time From: https://www.cnblogs.com/wzh366/p/17693847.html