When writing decorators especially when it's one that needs arguments other than the function to be wrapped, it often gets rather ugly...
Whhy not create a (meta-)decorator to do this? Something like:
To follow up on my untested suggestion, here's one that is tested:
# This metadecorator hasn't changed
def decorator_withargs(decf): def decorator(*args, **kwargs): def decorated(f): return decf(f, *args, **kwargs) return decorated return decorator
This is equivalent to: (1) decorator_withargs= partial( partial, prepartial )
[where partial is as in functools and prepartial(f, x, y)(z, t) <=> f(z, t, x, y)]
Indeed, and if one restricts decorator_withargs to keyword arguments, one can simply define it as:
decorator_withargs = partial(partial, partial)
I believe you can put -f- in the last pos'l argument and have this work.
def mydec( arg0, arg1, f, before='entering %s', after='%s returns %%s').
Which is the curry operator! So decorator_withargs is some sort of curry. In fact I had never realised before that this was a way to define curry (for functions with 2 arguments)
curry = partial(partial, partial)
[if f is a two-arguments function then curry(f)(x)(y) is f(x, y)]
This means that a meta-decorator (i.e. a decorator for decorators) is a kind of currying operator.
def f( callback, *bar, **bkwar ): def preg ( callfore, *far, **fkwar ): sf= g( callback, callfore, bar, bkwar, far, fkwar ) return sf return preg
We see how to rewrite this one?