I have this class:
Code:
class TClient:
def __init__(self, options):
self.__init__(server=options.server, port=options.port, user=options.username, password=options.password)
def __init__(self, server="127.0.0.1", port=8080, http_proxy="\"\"", username="", password=""):
self.server = server
self.port = str(port)
self.http_proxy = http_proxy
self.username = username
self.password = password
Is this the correct (python) way to call another constructor of the same class?
Anyway, I first used this line to instantiate the class TClient:
Code:
client = TClient(server=opts.server, port=opts.port, username=opts.username, password=opts.password)
and it's working ok. However, when I try (the variable opts is an OptionParser):
Code:
client = TClient(options=opts)
I though it should call the constructor with the single argument. Python says:
Code:
TypeError: __init__() got an unexpected keyword argument 'options'
Meaning, python tries to pass the opts argument to the wrong constructor (the long one), although I specified the name of argument. If I use:
Code:
client = TClient(opts)
(not naming the argument), python silently calls the wrong constructor.
I already solved the problem another way, but I would like to know why is this behavior occurring. Any thoughts?
Bookmarks