On Fri, Jul 1, 2016 at 4:08 PM, Lawrence D’Oliveiro <lawrenced...@gmail.com> wrote: > On Tuesday, June 28, 2016 at 5:03:08 PM UTC+12, Ben Finney wrote: >> There is a clever one-line decorator that has been copy-pasted without >> explanation in many code bases for many years:: >> >> decorator_with_args = lambda decorator: lambda *args, **kwargs: lambda >> func: decorator(func, *args, **kwargs) >> > > For those who want docstrings, I’ll give you docstrings: > > def decorator_with_args(decorator) : > "given function decorator(func, *args, **kwargs), returns a decorator > which," \ > " given func, returns the result of decorator(func, *args, **kwargs)." > > def decorate(*args, **kwargs) : > > def generated_decorator(func) : > return \ > decorator(func, *args, **kwargs) > #end generated_decorator > > #begin decorate > generated_decorator.__name__ = > "decorator_{}".format(decorator.__name__) > generated_decorator.__doc__ = "decorator which applies {} to the > previously-specified arguments".format(decorator.__name__) > return \ > generated_decorator > #end decorate > > #begin decorator_with_args > decorate.__name__ = "decorate_with_{}".format(decorator.__name__) > decorate.__doc__ = "generates a decorator which applies {} to the > given arguments".format(decorator.__name__)
You should use functools.wraps instead of clobbering the decorated function's name and docstring: @functools.wraps(decorator) def decorate(*args, **kwargs): ... > return \ > decorate Just to satisfy my own curiosity, do you have something against putting the return keyword and the returned expression on the same line? -- https://mail.python.org/mailman/listinfo/python-list