title: How does Flask knows which decorated function to call?
body: <p>So I'm going through the basic Flask tutorial, and looking at their code there's this snippet:</p>
  
  <pre><code> @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()
  </code></pre>
  
  <p>Now, for what I've read in their manual, the function "app.teardown_appcontext" is called whenever one of the callbacks has an unexpected behavior. Decorating a function with it allows you to add functionality to the original function. Or at least that's what I understand from decorators. But, if I do this:</p>
  
  <pre><code>@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()
  </code></pre>
  
  <p>It still works. How does Flask manages this? </p>
  
  <p>My guess is that when I run the "flaskr.py" file with all the main code, it associates whatever decorated function with their code to call it when necessary. I tried reading a lot of sources about decorator to see if my interpretation is wrong, but could not find any mistake.</p>