[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>需求?多个装饰器作用于一个函数,比如有两个装饰器@login, @allow,想先判断用户是否登陆,然后再判断用户是否有权限
<>一个示例解释python装饰器的执行顺序(抄别人的),这里有个问题所以记下来
def decorator_a(func): print('Get in decorator_a') def
inner_a(*args,**kwargs): print('Get in inner_a') res = func(*args,**kwargs)
return res return inner_a def decorator_b(func): print('Get in decorator_b')
def inner_b(*args,**kwargs): print('Get in inner_b') res = func(*args,**kwargs)
return res return inner_b @decorator_b @decorator_a def f(x): print('Get in f')
return x * 2
结果
Get in decorator_a
Get in decorator_b
<>
看起来,装饰器在函数定义时已经开始倒序执行。这里如果是用decorator_b(decorator_a(f)),得到的结果也是同样的。但这里有个问题,如果我再新建一个带参数的装饰器C
def decorator_c(nums): print("Get in decorator_c") def wrapper(func):
print("Get in wrapper_c") def inner_c(*args, **kwargs): print('Get in inner_c')
res = func(*args, **kwargs)+nums return res return inner_c return wrapper
@decorator_c(10) @decorator_b @decorator_a def f(x): print('Get in f') return x
* 2
<>这时候得到的执行结果是
Get in decorator_c
Get in decorator_a
Get in decorator_b
Get in wrapper_c
<>这是定义时的执行顺序,执行时的循序才是最重要的,执行f(1).
Get in inner_c
Get in inner_b
Get in inner_a
Get in f
很明显,经过一系列的装饰器定义,现在的函数大概可以这样表示:inner_c(… return inner_b(… return
inner_a(…return f(1)))), 在inner_c中返回的func是inner_b,inner_b返回的是inner_a,
inner_a返回f,inner_c —>inner_b —>inner_a ,顺序执行,inner_c的装饰部分代码首先执行。
所以如果需要实现,先登陆再判断权限,应该
@login
@allow
def get_user_department():
pass