Jonathan Fine wrote:
I also find writing decorators a bit hard. It seems to be something I have to learn anew each time I do it. Particularly for the pattern
@deco(arg1, arg2) def fn(arg3, arg4): # function body
Perhaps doing something with partial might help here. Anyone here interested in exploring this?
I can't think of a way that partial would help. But would you find it easier if you could do something like this?
def __init__(self, arg1, arg2): self.arg1 = arg1 self.arg2 = arg2
def invoke(self, func, arg3, arg4): # function body
def __call__(self, func): self._wrapped_function = func return self._wrapper
def _wrapper(self, *args, **kwds): return self.invoke(self._wrapped_function, *args, **kwds)