title: Why binding to context is necessary in Werkzeug
body: <p>I was reading the source code of the <a href="https://github.com/mitsuhiko/werkzeug" rel="nofollow">Werkzeug</a> library in github and in one of the examples (<a href="https://github.com/mitsuhiko/werkzeug/blob/master/examples/simplewiki" rel="nofollow">Simplewiki</a> to name it), in the <code>application.py</code> file there is function which binds the application to the current active context. I would like to know why this is necessary, or where can I find something that explains this?</p>
  
  <p>The function is this:</p>
  
  <pre><code>def bind_to_context(self):
          """
          Useful for the shell.  Binds the application to the current active
          context.  It's automatically called by the shell command.
          """
          local.application = self
  </code></pre>
  
  <p>And this is the part where the dispatcher binds the request.</p>
  
  <pre><code>def dispatch_request(self, environ, start_response):
          """Dispatch an incoming request."""
          # set up all the stuff we want to have for this request.  That is
          # creating a request object, propagating the application to the
          # current context and instanciating the database session.
          self.bind_to_context()
          request = Request(environ)
          request.bind_to_context()
  </code></pre>