atom feed6 messages in org.python.python-ideas[Python-ideas] An easier syntax for w...
FromSent OnAttachments
Adam AtlasOct 7, 2007 8:33 pm 
Brett CannonOct 7, 2007 8:49 pm 
Greg EwingOct 8, 2007 12:45 am 
Arnaud DelobelleOct 8, 2007 2:57 am 
Arnaud DelobelleOct 8, 2007 10:42 am 
Adam AtlasOct 9, 2007 8:40 pm 
Subject:[Python-ideas] An easier syntax for writing decorators (& similar things)?
From:Adam Atlas (
Date:Oct 7, 2007 8:33:51 pm

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?