Можно сделать так, взято с http://http://code.activestate.com/recipes/langs/python/:
Код | class A(object): def func(self): print self.__class__.__name__
def func1(self): print "func1"
def func2(self): print "func2"
def add_method_to_one(self, method, name=None): if name is None: name = method.func_name class new(self.__class__): pass setattr(new, name, method) self.__class__ = new
def add_method_to_all(self, method, name=None): if name is None: name = method.func_name setattr(self.__class__, name, method)
a = A() b = A()
a.func() b.func() print
add_method_to_all(b, func2, "func") a.func() b.func() print
add_method_to_one(a, func1, "func") a.func() b.func()
|
|