[Python-ideas] An easier syntax for writing decorators (& similar things)?

Brett Cannon brett at python.org
Mon Oct 8 05:49:45 CEST 2007


On 10/7/07, Adam Atlas <adam at atlas.st> wrote:
> When writing decorators especially when it's one that needs arguments
> other than the function to be wrapped, it often gets rather ugly...
>
> def dec(a, b, foo=bar):
>      def inner(func):
>          def something(*a, **k):
>              ...stuff...
>              return func(*a, **k)
>          return something
>      return inner
>
> Perhaps we could allow functions to be defined with multiple argument
> lists, basically partially applying the function until all of them
> are filled. (Sort of like currying, but sort of not.)
>
> def dec(a, b, foo=bar)(func)(*a, **k):
>      ...stuff...
>      return func(*a, **k)
>
> So, calling `dec` will fill the first argument list and return a
> callable, which when called will fill the second argument list and
> return a third callable, which will be the fully-decorated function.
> Basically, exactly as it looks -- def func(a)(b)(c) is called as func
> (1)(2)(3). Except, obviously, you can partially apply it by only
> calling the first one or two or however many. I'm not sure how this
> would look internally, but I imagine each successive call would
> return an object something like a partial.
>
> I expect that the main argument against this will be that it is not a
> common enough idiom to warrant adding syntax. Perhaps; I don't know.
> The decorator pattern is very useful (and not only in the @blah
> function decorator sense -- also the future class decorators, WSGI
> middleware, etc.), and I do think it makes their definitions quite a
> bit nicer and easier to read. Any thoughts?

My argument against it is it's ugly and hard to notice.  It is not
obvious unless you really look carefully at the signature to notice
that there are three sets of parentheses there.

-Brett



More information about the Python-ideas mailing list