I need a way to add a method to an existing instance, but be as close as
possible to normal instance methods. Using 'new' module or such code as
'def addfunc(...): def helper(...) .. setattr(...)' causes a cyclic
reference which requires using 'gc.collect' to release the object. Also
'new' is deprecated. I also made a helper that uses weakref, but the
problem is when the object is gone, existing instance method object
would not work:
f = myobject.function
myobject = None
f() <--- would not work if it holds weak ref to myobject
The best I can come up with is to create a parent class and try to
simulate as best as possible. The code below works with no cyclic
references that would be cause by 'new.instancemethod' etc, and without
the weakref as well. Is there a simpler way of achieving the same
without cyclic references? I know this is 'monkey patching' but for a
small project I'm working on it is useful to be able to add functions
dynamically to one instance without adding it to another instance of the
same class, etc.
class InstanceFunctionHelper(object):
class FunctionCaller(object):
def __init__(self, instance, func):
self.__instance = instance
self.__func = func
def __call__(self, *args, **kwargs):
return self.__func(self.__instance, *args, **kwargs)
def add_instance_function(self, func, name = None):
if name is None:
name = func.__name__
if hasattr(self, name):
delattr(self, name)
try:
self._instance_funcs[name] = func
except AttributeError:
self._instance_funcs = {name: func}
def __setattr__(self, name, value):
funcs = getattr(self, '_instance_funcs', None)
if funcs and name in funcs:
del funcs[name]
object.__setattr__(self, name, value)
def __delattr__(self, name):
funcs = getattr(self, '_instance_funcs', None)
if funcs and name in funcs:
del funcs[name]
else:
object.__delattr__(self, name)
def __getattr__(self, name):
if name == '_instance_funcs':
raise AttributeError
funcs = object.__getattribute__(self, '_instance_funcs')
if name in funcs:
return InstanceFunctionHelper.FunctionCaller(self, funcs[name])
raise AttributeError
x = 0
class Blah(InstanceFunctionHelper):
def __init__(self):
global x
x += 1
def __del__(self):
global x
x -= 1
def Action(self, value):
print self
print value
a = Blah()
a.add_instance_function(Action)
a.Action(5)
a = None
print x
--
http://mail.python.org/mailman/listinfo/python-list