下面是我的例子:
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()
当我运行127.0.0.1:5000时,我希望得到“hello11”,但我得到“hello21”,为什么?在
问题是Flask按名称跟踪函数,因为传递给
app.route(path)
的函数都被称为inner
,第二个函数(hello2
)覆盖了第一个函数。要解决这个问题,您需要将inner
函数的名称更改为它所装饰的函数的名称。你可以把装饰工换成这是可行的,但不如标准库解决方案优雅
^{pr2}$wraps
修饰符不仅修复了名称,还修复了docstring、文件和属性字典。在相关问题 更多 >
编程相关推荐