[Python-ideas] Problems (and solutions?) in writing decorators

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Oct 25 18:04:17 EDT 2018

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?

     class deco(Decorator):

         def __init__(self, arg1, arg2):
             self.arg1 = arg1
             self.arg2 = arg2

         def invoke(self, func, arg3, arg4):
             # function body


     class Decorator:

         def __call__(self, func):
             self._wrapped_function = func
             return self._wrapper

         def _wrapper(self, *args, **kwds):
             return self.invoke(self._wrapped_function, *args, **kwds)


More information about the Python-ideas mailing list