Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible not to use Flask decorators?

I don’t know why, but I don’t like decorators. Can I use

def load_user():
    if "user_id" in session:
        g.user = db.session.get(session["user_id"])

app.before_request(load_user)

instead of

@app.before_request
def load_user():
    if "user_id" in session:
        g.user = db.session.get(session["user_id"])

?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A decorator returns a new function, which the @decorator syntax assigns to the function being defined. Without using that syntax you need to reassign the function name.

def load_user():
    if "user_id" in session:
        g.user = db.session.get(session["user_id"])

load_user = app.before_request(load_user)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading