Instrospection question

Matias Surdi matiassurdi at gmail.com
Fri Dec 21 05:28:21 EST 2007


I have the following code:

--------------------------
import new

class A:
     def a(self):
         print "Original"

def other(cad):
     return cad + " modified"

def replace_method(method):
     def b(self,*args,**kwargs):
         result = method(*args,**kwargs)
         return other(result)
     return b


a = A()

setattr(a,"a",new.instancemethod(replace_method(a.a) ,a,A))

a.a()

#Result should be:
# Original modified

------------------------------

As you can see, what I'm trying to do is "replace" a method with another 
one wich is the same method but with a function applied to it (in this 
case, a string concatenation ( +" modified"))

Can anybody help me with this?







More information about the Python-list mailing list