I'm fairly new to Python and currently I'm running a few examples to understand how things work, but I got stuck in one of the steps.
I have a class with several attributes in it, and an __init__(self)
constructor, something like this:
class A:
# Some attributes here
a = None;
b = None;
...
def __init__(self):
# Do some stuff here
Further, I have another class which inherits from 2 classes: the above A
and SocketServer.BaseRequestHandler
(library).
class TCPHandler(A, SocketServer.BaseRequestHandler):
def handle(self):
# Do some other stuff here
Basically this will trigger the handle()
method once it receives a TCP request, but my question is much simpler than that. When I declare a TCPHandler
object, I do it that way:
server = SocketServer.TCPServer('localhost', 9191), TCPHandler)
server.serve_forever()
This, however, seems to try invoking an A
class' constructor with 4 arguments (which I presume the SocketServer.BaseRequestHandler
has):
TypeError: __init__() takes exactly 1 argument (4 given)
So in A
I added another constructor with 4 arguments:
def __init__(self, a=None, b=None, c=None, d=None):
# Do some other stuff here
Now this constructor seems to be triggered, however, the RequestHandler.handle()
method is never called upon an arriving TCP connection.
Why is the handle()
method never called in this situation?
---- EDIT ----
This would be a short and complete program that would illustrate the behavior I explained:
import sys
import SocketServer
class A:
a = ''
b = ''
c = ''
def load_config(self):
a = 'a'
b = 'b'
c = 'c'
def __init__(self):
self.load_config()
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True, debug=True):
self.load_config()
class TCPHandler(A, SocketServer.BaseRequestHandler):
def handle(self):
print "I reached handle()!!"
if __name__ == "__main__":
server = SocketServer.TCPServer(('localhost', 9999), TCPHandler)
server.serve_forever()
- If the 5 argument constructor wasn't added, the error above would be shown.
- If the 5 argument constructor is added, the
I reached handle()!!
message is never shown (therefore,handle()
is not being triggered).