SocketServer.BaseRequestHandler and __init__/super()

Tim Chase python.list at tim.thechases.com
Thu May 17 20:32:18 EDT 2012


Sparring with a little sandbox/test code (in 2.6, FWIW), I'm trying
to set up some instance variables in my __init__ but keep hitting my
head against the wall.
Initially, I had something of the form
 class MyServer(SocketServer.BaseRequestHandler):
 def __init__(self, *args, **kwargs):
 self.foo = "some default"
 super(MyServer, self).__init__(*args, **kwargs)
but, because SocketServer.BaseRequestHandler is an old-style class,
it barfed with the classic "TypeError: super() argument 1 must be
type, not classobj"
So I tossed in "object" to the class hierarchy:
 class MyServer(object, SocketServer.BaseRequestHandler):
 def __init__(self, *args, **kwargs):
 self.foo = "some default"
 super(MyServer, self).__init__(*args, **kwargs)
but now because it tries to pass *args/**kwargs into object.__init__
I get a "TypeError: object.__init__() takes no parameters"
I know I can just force it with
 class MyServer(SocketServer.BaseRequestHandler):
 def __init__(self, *args, **kwargs):
 self.foo = "some default"
 SocketServer.BaseRequestHandler.__init__(self, *args, **kwargs)
but I want to be sure there's not some more pythonicly proper way to
go about it. Any thoughts on this? (other than "SocketServer should
have inherited from object which is a 2.x best-practice")
-tkc


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /