Instrospection question

Duncan Booth duncan.booth at invalid.invalid
Fri Dec 21 06:19:52 EST 2007


Matias Surdi <matiassurdi at gmail.com> wrote:

> 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.__dict__['a'] = new.instancemethod(replace_method(a.a),a,A)

> 
> a.a()
> 
> #Result should be:
> # Original modified
> 
You are unlikely to get that unless you make 'a' return a result. I get:

>>> a.a()
Original

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    a.a()
  File "<pyshell#5>", line 4, in b
    return other(result)
  File "<pyshell#3>", line 2, in other
    return cad + " modified"
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'




More information about the Python-list mailing list