I want to start out by saying I'm aware new.instancemethod
doesn't exist in python 3 and overloading functions doesn't technically either.
However, I am looking at an old python package which uses new.instancemethod
in a script that effectively allows for overloaded functions
.
The specific package can be found from this site although I've included what I believe is the relevant section below.
I am using python 3 so I need to convert it into a usable script for python 3. I've used the 2to3 tool and reviewed most of the changes already. However, I am stuck on the below which I can't figure out what to replace new.instancemethod
with.
class overloaded:
...
def __get__(self, obj, type=None):
if obj is None:
return self
return new.instancemethod(self, obj)
Class overloaded is used in front of __init__
as a decorator which is posing no problems, but it is a problem when it is used in front of a method because obj
is no longer None
.
The reason I need this, is because a) it is useful and b) because I am currently translating the out-of-date IbPy package from 2.56 to 3.x. The IbPy package is basically the Java API of Interactive Brokers translated into Python.
The main method that @overloaded is placed in front of can be found below:
@overloaded
@synchronized(mlock)
def eConnect(self, host, port, clientId):
host = self.checkConnected(host)
if host is None:
return
try:
socket = Socket(host, port)
self.eConnect(socket, clientId)
except Exception as e:
self.eDisconnect()
self.connectionError()
Note that @synchronize is just a decorator that forces synchronization of multiple functions. The code is short and can be found here, although I don't believe it is relevant for my issue.
So after that long explanation, how would I get return new.instancemethod(self, obj)
to look more python 3? I can't seem to wrap my head around what is happening despite reading about it for all of last night and this morning. I've tried many things already, and I think I just lack the understanding in python of how to handle this.
Any advice would be greatly appreciated. Thanks!