[Python-ideas] Syntax for defining parametric decorators

Stefan Behnel stefan_ml at behnel.de
Mon Jul 9 11:47:48 CEST 2012


Mike Graham, 08.07.2012 22:22:
> A common stumbling block for new users is writing decorators that take
> arguments. To create a decorator like
> 
> @timesn(n)
> def f(y):
>     ...
> 
> We write code like
> 
> def timesn(n)
>     def decorator(f):
>         def inner(y):
>             return n * f(y)
>         return inner
>      return decorator
> 
> which confuses many users and can be a handful to type. I wonder if it
> would be clearer for people to write
> 
> def timesn(n)(f):
>     def inner(y):
>         return n * f(y)
>     return inner
> 
> 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.

>From an innocent look, I have no idea what the syntax is supposed to mean.
Clearly doesn't hint at a factory for me.

Stefan




More information about the Python-ideas mailing list