dcc*_*rmo 3 python flask python-decorators
所以我正在阅读基本的Flask教程,看看他们的代码就是这个片段:
@app.teardown_appcontext
def close_db_connection(exception):
"""Closes the database again at the end of the request."""
top = _app_ctx_stack.top
if hasattr(top, 'sqlite_db'):
top.sqlite_db.close()
Run Code Online (Sandbox Code Playgroud)
现在,对于我在他们的手册中读到的内容,只要其中一个回调出现意外行为,就会调用"app.teardown_appcontext"函数.使用它装饰功能允许您向原始功能添加功能.或者至少这是我对装饰者的理解.但是,如果我这样做:
@app.teardown_appcontext
def stack_overflow_rocks(exception):
"""Closes the database again at the end of the request."""
top = _app_ctx_stack.top
if hasattr(top, 'sqlite_db'):
top.sqlite_db.close()
Run Code Online (Sandbox Code Playgroud)
它仍然有效.Flask如何管理这个?
我的猜测是,当我运行带有所有主要代码的"flaskr.py"文件时,它将任何装饰函数与其代码相关联,以便在必要时调用它.我试着阅读很多关于装饰的消息来源,看看我的解释是否错误,但是找不到任何错误.
| 归档时间: |
|
| 查看次数: |
604 次 |
| 最近记录: |