Wrapping a method twice
marek.rocki at wp.pl
marek.rocki at wp.pl
Wed Jun 25 07:52:07 EDT 2008
Nicolas Girard napisał(a):
> prepend(C.f,pre)
This wraps f, returns wrapped and binds it to C.f (but the
method.__name__ is still wrapped).
> append(C.f,post)
This wraps wrapped (the one previously returned), returns wrapped (a
new one) and binds it to C.wrapped (since that is what its
method.__name__ says).
> C().f()
If you try C().wrapped() it works as expected.
So the problem is in retaining the function name. Try:
def append(method,bottom):
def wrapped(*args, **kwargs):
res = method(*args, **kwargs)
return bottom(res,*args, **kwargs)
wrapped.__name__ = method.__name__
setattr(method.im_class,method.__name__,wrapped)
def prepend(method,top):
def wrapped(*args, **kwargs):
top(*args, **kwargs)
return method(*args, **kwargs)
wrapped.__name__ = method.__name__
setattr(method.im_class,method.__name__,wrapped)
More information about the Python-list
mailing list