Wrapping a method twice

Nicolas Girard ng at invalid.fr
Wed Jun 25 13:34:06 CEST 2008


Hi all,
given the following code:

---
def append(method,bottom):
    def wrapped(*args, **kwargs):
        res = method(*args, **kwargs)
        return bottom(res,*args, **kwargs)
    setattr(method.im_class,method.__name__,wrapped)

def prepend(method,top):
    def wrapped(*args, **kwargs):
        top(*args, **kwargs)
        return method(*args, **kwargs)
    setattr(method.im_class,method.__name__,wrapped)


def test():
    class C:
        def f(self):
            print "f"
    
    def pre(self):
        print "pre"
    
    def post(res,self):
        print "post"
    
    prepend(C.f,pre)
    append(C.f,post)   
    C().f()
---

how comes that test() only outputs:

  pre
  f

rather than what I expected:

  pre
  f
  post

Thanks very much in advance,
cheers,
Nicolas



More information about the Python-list mailing list