By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,610 Members | 1,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,610 IT Pros & Developers. It's quick & easy.

Adding functions to an existing instance

P: n/a
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

Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On 26 juin, 17:18, Allen <brian_vanderbu...@yahoo.comwrote:
I need a way to add a method to an existing instance, but be as close as
possible to normal instance methods.
def set_method(obj, func, name=None):
if not name:
name = func.__name__
setattr(obj, name, func.__get__(obj, type(obj)))

class Toto(object):
pass

toto = Toto()

def titi(self):
print self

set_method(toto, titi)

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.