Wrapping a method twice

Michele Simionato michele.simionato at gmail.com
Wed Jun 25 14:27:35 CEST 2008


On Jun 25, 1:52 pm, marek.ro... at wp.pl wrote:
>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)

If you are using Python 2.5, consider functools.update_wrapper, which
also sets __module__,
__doc__ and other attributes.



More information about the Python-list mailing list