[mod_python] using callable classes instead of plain functions in handlers

Daniel Popowich dpopowich at comcast.net
Mon Sep 27 10:04:27 EDT 2004


Martin Slouf writes:
> it would be _very_ nice if one could just create a class, use its () operator
> (the __call__() method) and put some standard behavior in that call.  then
> create an object of this class and publisher handler with request
> http://somewhere.org/index.py/my_handler
> would instead of a function 'my_handler' defined in 'index.py' would call my
> callable object 'my_handler' created in 'index.py'
> 
> the MyHandler class could look like this:
> 
> class MyHandler:
>       def __call__(req, ...):
> 	  self._handle_errors()
> 	  self._do_some_special_stuff() # ready to override
> 	  self._log_request()
> 	  self._generate_html()
> 
> if i want a special behaviour, no problem -- just subclass 'MyHandler' and
> change the default behaviour.
> 
> anyone has tried that?  are there any frameworks using it?  is it slow?

You may want to take a look at mod_python servlets (link below,
complete with on-line tutorial) where instances of classes handle each
request.  If all your servlets subclass (or mix-in) from a class that
has such behaviour, then you're golden.

Daniel Popowich
-----------------------------------------------
http://home.comcast.net/~d.popowich/mpservlets/



More information about the Mod_python mailing list