Adding functions to an existing instance

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Thu Jun 26 14:26:45 EDT 2008


On 26 juin, 17:18, Allen <brian_vanderbu... at yahoo.com> wrote:
> 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)




More information about the Python-list mailing list