Class extension confusion :(

Teenan t33n4n at gmail.com
Wed Nov 10 15:27:40 CET 2010


If memory serves, the following should work fine, as long as your happy for
these vars to have the same value for all instances of the RequestHandler
(static)

MyHandler = PlainAJAXRequestHandler
MyHandler.paths = my_paths_var
webServer = HTTPServer( server_address, MyHandler)

An alternative I've used in the past is to extend the server class in the
same way, and store these vars in the server rather than the request
handler.
You can then use  self.server in the requesthandler which holds the server
instance.
like   self.server.my_var

I seem to remember the documentation saying something about  that the
__init__ shouldn't be overriden for the HTTPRequestHandler class, so I
wouldn't go down that route.

Teenan.

On Wed, Nov 10, 2010 at 5:45 AM, r0g <aioe.org at technicalbloke.com> wrote:

> I have a subclass of BaseHHTPRequestHandler which uses a dictonary "paths"
> and a function "api_call" which are defined in the main namespace of the
> module. I'd rather I was able to pass these object to the constructor and
> store them as data attributes "self.paths" and "self.api_call" but I'm not
> sure how to do that properly. My understanding is that one may extend a
> constructor by defining it's __init__ method, calling the parents
> constructor and then adding ones own attributes to taste. What I don't
> understand is where or how I am supposed to get these extra constructor
> arguments into the class given that I don't instantiate it myself, it is
> seemingly instantiated by HTTPServer class that I pass it to e.g.
>
> httpd = HTTPServer(server_address, PlainAJAXRequestHandler)
>
> I wondered if I ought to instantiate an instance of
> PlainAJAXRequestHandler, set the attributes (either manually or by extending
> it's constructor) and pass that to HTTPServer but I figured it expects a
> class not an instance as it probably wants to spawn one instance for each
> request so that would be a non starter. Might I need to subclass HTTPServer,
> find the bit that instantiates the request handler and override that so it
> passes it's constructor more parameters? Right now I'm pretty confused, can
> somebody please tell me how I might accomplish this, what I'm failing to
> grasp or point me to the docs that explain it - I've spent the last hour or
> two plowing through docs to no avail, I guess it's a case of keyword
> ignorance on my part! Code follows...
>
> Thanks for reading!
>
> Roger.
>
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101110/afebb10a/attachment.html>


More information about the Python-list mailing list