[Python-ideas] Problems (and solutions?) in writing decorators
sylvain.marie at se.com
Tue Mar 12 05:36:41 EDT 2019
Dear python enthusiasts,
Writing python decorators is indeed quite a tideous process, in particular when you wish to add arguments, and in particular in two cases : all optional arguments, and one mandatory argument. Indeed in these two cases there is a need to disambiguate between no-parenthesis and with-parenthesis usage.
After having struggled with this pattern for two years in various open source and industrial projects, I ended up writing a library to hopefully solve this once and for all: https://smarie.github.io/python-decopatch/ . It is extensively tested (203 tests) against many combinations of signature/calls.
I would gladly appreciate any feedback !
Please note that there is a "PEP proposal draft" in the project page because I belive that the best a library can do will always be a poor workaround, where the interpreter or stdlib could really fix it properly.
Sorry for not providing too much implementation details in that page, my knowledge of the python interpreter is unfortunately quite limited.
Finally there is an additional topic around decorators : people tend to believe that decorators and function wrappers are the same, which is absolutely not the case. I used the famous `decorator` lib in many projects but I was not satisfied because it was solving both issues at the same time, maintaining the confusion. I therefore proposed https://smarie.github.io/python-makefun/ . In particular it provides an equivalent of `@functools.wraps` that is truly signature-preserving (based on the same recipe than `decorator`).
Once again, any feedback would be gladly appreciated !
De : Python-ideas <python-ideas-bounces+sylvain.marie=se.com at python.org> De la part de Greg Ewing
Envoyé : vendredi 26 octobre 2018 00:04
À : python-ideas <python-ideas at python.org>
Objet : Re: [Python-ideas] Problems (and solutions?) in writing decorators
[External email: Use caution with links and attachments]
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
def _wrapper(self, *args, **kwds):
return self.invoke(self._wrapped_function, *args, **kwds)
Python-ideas mailing list
Python-ideas at python.org
Code of Conduct: https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F&data=02%7C01%7Csylvain.marie%40se.com%7C295cecd58fc3461f42c108d63ac5e03e%7C6e51e1adc54b4b39b5980ffe9ae68fef%7C0%7C0%7C636761018888615894&sdata=u0hvM5cR%2BR1Vni%2BV48WoNF%2FpriCaOG5%2BFAXayaTGsYY%3D&reserved=0
This email has been scanned by the Symantec Email Security.cloud service.
More information about the Python-ideas