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 !
-----Message d'origine----- De : Python-ideas firstname.lastname@example.org De la part de Greg Ewing Envoyé : vendredi 26 octobre 2018 00:04 À : python-ideas email@example.com 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 return self._wrapper
def _wrapper(self, *args, **kwds): return self.invoke(self._wrapped_function, *args, **kwds)
-- Greg _______________________________________________ Python-ideas mailing list Pythonfirstname.lastname@example.org https://emea01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.pyth... Code of Conduct: https://emea01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fpython.org...
______________________________________________________________________ This email has been scanned by the Symantec Email Security.cloud service. ______________________________________________________________________