[Python-ideas] Syntax for curried functions
Steven D'Aprano
steve at pearwood.info
Mon Mar 2 13:09:57 CET 2009
Weeble wrote:
> Would this be crazy? Make this:
>
> def spam(a,b,c)(d,e,f):
> [BODY]
>
> a synonym for:
>
> def spam(a,b,c):
> def spam2(d,e,f):
> [BODY]
> return spam2
>
> As far as I can tell, it would always be illegal syntax right now, so
> it seems a safe change. And it would make it cleaner to write higher
> order functions.
I don't think it would. Your proposed syntax is only useful in the case
that the outer function has no doc string and there's no pre-processing
or post-processing of the inner function, including decorators.
In practice, when I write higher order functions, I often do something
like this example:
# untested
def spam_factory(n):
"""Factory returning decorators that print 'spam' n times."""
msg = "spam " * n
if n > 5:
msg += 'WONDERFUL SPAM!!!'
def decorator(func):
"""Print 'spam' %d times."""
@functools.wraps(func)
def f(*args, *kwargs):
print msg
return func(*args, **kwargs)
return f
decorator.__doc__ = decorator.__doc__ % n
return decorator
Although the function as given is contrived, the basic structure
(including doc strings, decorators, pre-processing and post-processing)
is realistic, and your suggested syntax wouldn't be useful here.
--
Steven
More information about the Python-ideas
mailing list