Here is my example:
from flask import Flask
app = Flask(__name__)
def add1(f):
def inner(*args, **kwargs):
return str(f(*args, **kwargs))+'1'
return inner
@app.route('/')
@add1
def hello1():
return "hello1";
@app.route('/hello2')
@add1
def hello2():
return "hello2";
if(__name__ =='__main__'):
app.run()
When I run 127.0.0.1:5000, I expect to get “hello11”, but I get “hello21”, why?
The problem is that Flask keeps track of the functions by name, and because the functions getting passed to
app.route(path)are both calledinner, the second one (hello2) overwrites the first. To fix this, you will need the name of theinnerfunction to be changed to the name of the function it is decorating. You can change the decorator towhich will work, but is not as elegant as the standard library solution,
The
wrapsdecorator not only fixes the name, but the docstring, the file, and the attribute dictionary.