junjie 通过本文主要向大家介绍了python合并多个文件,python 多个列表合并,python 返回多个值,python读取多个文件,python打开多个文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
django程序,需要写很多api,每个函数都需要几个装饰器,例如
@csrf_exempt
@require_POST
def foo(request):
pass
</div>
既然那么多个方法都需要写2个装饰器,或者多个,有啥办法把多个合并成一行呢?
上面的函数执行过程应该是
csrf_exempt(require_POST(foo))
</div>
修改成
def compose(*funs):
def deco(f):
for fun in reversed(funs):
f = fun(f)
return f
return deco
</div>
函数改写成
@compose(csrf_exempt, require_POST)
def foo(request):
pass
</div>
参考:
Can I combine two decorators into a single one in Python

