On 18Nov2019 19:09, Samuel Muldoon <[email protected]> wrote:
At present, multi-argument function decorators are a little bit tricky to
implement.
Aye, but one can write a decorator for decorators which makes it easy.
No extra syntax required.
I've one of my own called @decorator, available on PyPI from the
"cs.deco" module. Use:
from cs.deco import decorator
@decorator
def wait(func, delay=1.0):
def wrapped(*a, **kw):
if delay > 0:
time.sleep(delay)
return func(*a, **kw)
return wrapped
and I can then use this as:
@wait
def print_something(something):
print(something)
@wait(delay=0.2)
def print_something_else(something):
print(something)
as you desire.
So any multiargument decorator I write such as the above I write as
though it is unconditionally handed the function to wrap and the
parameters of the decorator, and the @decorator decorator produces an
outer decorator with the special logic you describe.
I'm arguing here that (a) you could just grab my @decorator (and I'm
sure there are other equivalent ones in PyPI) and (b) you don't need
extra syntax.
Of course, you could legitamtely argue that the above should be part of
the protocol which @ implements, and that any sufficiently flexible
decorator should expect a **kw. I'd support that. Again, no new syntax
required, just better behaviour!
Cheers,
Cameron Simpson <[email protected]>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/QCNHCJLKGOMIFNBD3WPSAPWKVFOU2FZN/
Code of Conduct: http://python.org/psf/codeofconduct/