[Python-ideas] Syntax for defining parametric decorators

Devin Jeanpierre jeanpierreda at gmail.com
Sun Jul 8 22:41:02 CEST 2012


On Sun, Jul 8, 2012 at 4:22 PM, Mike Graham <mikegraham at gmail.com> wrote:
> which is more concise and looks a lot more like a non-parametric
> decorator someone might have written already. The syntax is mostly
> self-explaining and could potentially be useful in other contexts.

Ooh, +1 Semantically the function and the decorator-params are both
"arguments" to the decorator, and this confuses a lot of people into
writing silly things like def decorator(arg1, arg2, f): ...

In actuality, these two are equivalent, and this new def syntax reflects that:

@decorator(arg):
def foo(...):
    ...

def foo(...):
    ...
foo = decorator(arg)(foo)

As opposed to the usual definition syntax, which is not really intuitive.

> One thing I didn't specify was whether `n` was nonlocal or not and the
> behavior of something that keeps and reuses timesn(some_specific_n)
> multiple times.

For the purposes of decorators I don't think it matters. I would guess
that keeping and reusing the first arg is more useful for
non-decorator purposes, and making them a nonlocal is more consistent
with the rest of Python, if we reuse the argument. Plus it'd align
better semantically with the old way of defining parametric
decorators.

-- Devin



More information about the Python-ideas mailing list