On Mon, Jun 18, 2012 at 6:49 PM, Emile van Sebille <em...@fenx.com> wrote: > On 6/18/2012 3:16 PM Roy Smith said... > class myDecorator(object): > def __init__(self, f): > self.f = f > def __call__(self): > print "Entering", self.f.__name__ > self.f() > print "Exited", self.f.__name__
I dunno about other people, but I generally avoid class decorators because they do not behave the way functions do when applied as a decorator for a method. This is a decorator taken out of some of my own source code. I was mostly dicking around with things, as opposed to actually coding. The docstring was even more ridiculous. from functools import reduce def rapply(*args): """apply(f, ...) <-> rapply(..., f)""" return args[-1](*args[:-1]) def condecorator(condition, *decorators): if condition: return lambda f: reduce(rapply, reversed(decorators), f) else: return lambda f: f Example usage: @condecorator(condition, decorator1, decorator2) def decorated(...): ... equivalent to: def decorated(...): ... if condition: decorated = decorator1(decorator2(decorated)) -- Devin -- http://mail.python.org/mailman/listinfo/python-list