Flask official tutorial: what is 'e' in close_db(e=None)?
Question:
In Flask’s tutorial [here], the definition for close_db included a default value for ‘e=None’. But, this value isn’t used in the function nor I can find reference anywhere.
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
Is there any specific reasons ‘e=None’ is explicitly stated?
Answers:
You’ll notice in the tutorial that in the init_app
function, close_db
is passed as an argument to app.teardown_appcontext()
.
From the docstring for teardown_appcontext
:
When a teardown function was called because of an unhandled exception
it will be passed an error object. If an errorhandler
is
registered, it will handle the exception and the teardown will not
receive it.
So e
refers to the error object, which is None by default. You can learn more about registering error handlers at this link.
In Flask’s tutorial [here], the definition for close_db included a default value for ‘e=None’. But, this value isn’t used in the function nor I can find reference anywhere.
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
Is there any specific reasons ‘e=None’ is explicitly stated?
You’ll notice in the tutorial that in the init_app
function, close_db
is passed as an argument to app.teardown_appcontext()
.
From the docstring for teardown_appcontext
:
When a teardown function was called because of an unhandled exception
it will be passed an error object. If anerrorhandler
is
registered, it will handle the exception and the teardown will not
receive it.
So e
refers to the error object, which is None by default. You can learn more about registering error handlers at this link.