On Wed, Feb 22, 2017 at 8:16 AM, Pavol Lisy <pavol.l...@gmail.com> wrote: > Maybe this technique could be reusable (and maybe part of functools?) > > With this decorator: > > def wrap_args(decorator): > def decor_out(*args, **kwargs): > def decor_in(func): > return decorator(func, *args, **kwargs) > return decor_in > return decor_out > > Alfredo needs to change only (*) next 2 lines: > > def fun_cache(function): > memo = {} > > to: > > @wrap_args > def fun_cache(function, cache): > memo = cache > > (*) - Steve's improvements (for example using functools.wraps) are > good to consider as well! :) > (but maybe catching TypeError could more problems hide than solve)
When all the arguments are optional it's generally desirable that @foo and @foo() be equivalent. See: https://blogs.it.ox.ac.uk/inapickle/2012/01/05/python-decorators-with-optional-arguments/ I would augment that example by making the optional arguments keyword-only to prevent accidentally passing one as the function to be decorated. Hopefully this feature would also be part of any general reusable solution. -- https://mail.python.org/mailman/listinfo/python-list