How to group decorators in Python

 ̄綄美尐妖づ 提交于 2020-01-02 04:48:28

问题


In Flask I'm using a set of decorators for each route, but the code is "ugly":

@app.route("/first")
@auth.login_required
@crossdomain(origin='*')
@nocache
def first_page:
    ....

@app.route("/second")
@auth.login_required
@crossdomain(origin='*')
@nocache
def second_page:
    ....

I would prefer to have a declaration that groups all of them with a single decorator:

@nice_decorator("/first")
def first_page:
    ....

@nice_decorator("/second")
def second_page:
    ....

I tried to follow the answer at Can I combine two decorators into a single one in Python? but I cannot make it working:

def composed(*decs):
    def deco(f):
        for dec in reversed(decs):
            f = dec(f)
        return f
    return deco

def nice_decorator(route):
    composed(app.route(route),
             auth.login_required,
             crossdomain(origin="*"),
             nocache)

@nice_decorator("/first")
def first_page:
    ....

because of this error that I don't understand:

@nice_decorator("/first")
TypeError: 'NoneType' object is not callable

Following one of the comments I defined it with another form that works but without the possibility to specify the route parameter:

new_decorator2 = composed(app.route("/first"),
                          auth.login_required,
                          crossdomain(origin="*"),
                          nocache)

Is it possible to define a single decorator with parameters?


回答1:


You're not defining the composition correctly. You need to change the definition of nice_decorator to something like this:

def nice_decorator(route):
    return composed(
        app.route(route),
        auth.login_required,
        crossdomain(origin="*"),
        nocache
    )

Your previous version never actually returned anything. Python isn't like Ruby or Lisp where the last expression is the return value.



来源:https://stackoverflow.com/questions/46564463/how-to-group-decorators-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!