== A decorator for decorators that accept additional arguments Once you're happy with functions returning other functions (technically closures), basic ordinary decorators in Python are easy enough to use, write, and understand. You get things like: .pn prewrap on from functools import wraps def trace(func): @wraps(func) def _d(*args, **kwargs): print "start", func.__name__ try: return func(*args, **kwargs) finally: print "end", func.__name__ return _d @trace def jim(a, b): print "in jim" return a + b (If you're decorating functions that all have the same argument signature, you can make the _``_d()''_ closure take the specific arguments and pass them on instead of using the ((*args)) and ((**kwargs)) approach.) But sometimes you have decorators that want to take additional arguments (over and above the function that they get handed to decorate). The syntax for doing this when you're declaring functions that will get decorated is easy and obvious: @tracer("barney") def fred(a, b, c): print "in fred:", a return b - c Unfortunately the niceness stops there; an implementation of _tracer()_ is much more complicated than _trace()_. Because of how Python has defined things, _tracer()_ is no longer a decorator but a decorator factory, something that when called creates and returns the decorator that will actually be applied to _fred()_. You wind up with functions returning functions that return functions, or with _tracer()_ actually being a class (so that calling it creates an instance that when called will actually do the decorating). What we would like is for _tracer()_ to be a regular decorator that just has extra arguments. Well, we can do that; all we need is another decorator that we will use on _tracer()_ itself. Like so: from functools import partial def decor(decorator): @wraps(decorator) def _dd(*args, **kwargs): return partial(decorator, *args, **kwargs) return _dd @decor def tracer(note, func): fname = func.__name__ @wraps(func) def _d(*args, **kwargs): print "-> %s: start %s" % (note, fname) try: return func(*args, **kwargs) finally: print "-> %s: end %s" % (note, fname) return _d (You can make _tracer()_'s arguments have the function to be decorated first, but then you have to do more work because you can't use _functools.partial()_. While I think that _func_ belongs as the first argument, I don't quite feel strongly enough to give up _partial()_.) The one nit with this is that positional arguments really are positional and keyword arguments really are keywords. You can't, for example, write: > @tracer(note="greenlet") > def fred(....): > .... (The only way around this is changing the order of arguments to _tracer()_ so that _func_ is first, which means giving up the convenience of just using _partial()_.) I've been thinking about decorators lately (they're probably the right solution for a code structure problem) and had this issue come up in my tentative design, so I felt like writing down my solution for later use. I'm sure that regular users of decorators already know all of these tricks. (Decorators are one of the Python things I should use more. I don't for complex reasons that involve my history with significant Python coding.)