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

Adam Atlas adam at atlas.st
Mon Oct 8 05:33:51 CEST 2007

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):
             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):
     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?

More information about the Python-ideas mailing list