parameterized classes

John Tantalo john.tantalo at
Thu Jul 24 09:29:51 CEST 2008

Is there is a better way to create parameterized classes than defining and
returning a class in a closure? I ask because I want to create arbitrary
BaseRequestHandler subclasses that delegate line handling to a given line
handler, as in this example:
      from SocketServer import *

      class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass

      def LineRequestHandler(handler):
        class __LineRequestHandler(BaseRequestHandler):
          def handle(self):
            for line in self.lines():

          def lines(self):
            line = self.line()
            while line:
              yield line
              line = self.line()

          def line(self):
            parts = []
            while not parts or parts[-1] and not parts[-1][-1] == "\n":
            if parts:
              return ''.join(parts)[0:-1]

        return __LineRequestHandler

      class SomeLineHandler:
        def handle(self, line):
          print "I got a line: %s" % line

      if __name__ == '__main__':
        s = ThreadingTCPServer(("", 2005),

I really wish I could create a class whose instances were classes that
subclassed BaseRequestHandler. Is this possible, or is there a better way
than my approach here? Or am I crazy?

And I may be dense, so if there is an easier way to stream sockets as line
streams, please let me know. I honestly don't think it should be this
difficult to implement a socket handler this simple.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list