From: Ben Darnell Date: Mon, 26 Jul 2010 19:04:00 +0000 (-0700) Subject: Add an initialize() method to RequestHandler for use by subclasses. X-Git-Tag: v1.1.0~63 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=83864d60198c001cbe53fad657a42c2a7777ad1b;p=thirdparty%2Ftornado.git Add an initialize() method to RequestHandler for use by subclasses. This is simpler than overriding __init__ since it doesn't require knowledge of the application and request arguments that must be passed through to the superclass's __init__. --- diff --git a/tornado/web.py b/tornado/web.py index d2a8c4c98..2dd2686fa 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -83,7 +83,7 @@ class RequestHandler(object): """ SUPPORTED_METHODS = ("GET", "HEAD", "POST", "DELETE", "PUT") - def __init__(self, application, request): + def __init__(self, application, request, **kwargs): self.application = application self.request = request self._headers_written = False @@ -99,6 +99,27 @@ class RequestHandler(object): if hasattr(self.request, "connection"): self.request.connection.stream.set_close_callback( self.on_connection_close) + self.initialize(**kwargs) + + def initialize(self): + """Hook for subclass initialization. + + A dictionary passed as the third argument of a url spec will be + supplied as keyword arguments to initialize(). + + Example: + class ProfileHandler(RequestHandler): + def initialize(self, database): + self.database = database + + def get(self, username): + ... + + app = Application([ + (r'/user/(.*)', ProfileHandler, dict(database=database)), + ]) + """ + pass @property def settings(self):