Lambda as declarative idiom (was RE: what is lambda used for in real code?)

Steven Bethard steven.bethard at gmail.com
Mon Jan 3 13:54:06 EST 2005


Roman Suzi wrote:
> I wish lambdas will not be deprecated in Python but the key to that is
> dropping the keyword (lambda). If anybody could think of a better syntax for
> lambdas _with_ arguments, we could develop PEP 312 further.

Some suggestions from recent lambda threads (I only considered the ones 
that keep lambda as an expression):

***** Args Before Expression *****

Nick Coghlan: def-to syntax [1]
(def (a, b, c) to f(a) + o(b) - o(c))
(def (x) to x * x)
(def () to x)
(def (*a, **k) to x.bar(*a, **k))
((def () to x(*a, **k)) for x, a, k in funcs_and_args_list)

Nick Coghlan: def-arrow syntax [1]
(def (a, b, c) -> f(a) + o(b) - o(c))
(def (x) -> x * x)
(def () -> x)
(def (*a, **k) -> x.bar(*a, **k))
((def () -> x(*a, **k)) for x, a, k in funcs_and_args_list)

Alex Martelli: def-as syntax [2]
(def (a, b, c) as f(a) + o(b) - o(c))
(def (x) as x * x)
(def () as x)
(def (*a, **k) as x.bar(*a, **k))
((def () as x(*a, **k)) for x, a, k in funcs_and_args_list)

Dave Benjamin: fun syntax [7]
(fun(a, b, c): f(a) + o(b) - o(c))
(fun(x): x * x)
(fun(): x)
(fun(*a, **k): x.bar(*a, **k))
((fun(): x(*a, **k)) for x, a, k in funcs_and_args_list)


***** Expression Before Args *****

Robert Brewer: for (no-parens) syntax [3]
(f(a) + o(b) - o(c) for a, b, c)
(x * x for x)
(x for ())
(x.bar(*a, **k) for *a, **k)
((x(*a, **k) for ()) for x, a, k in funcs_and_args_list)

Nick Coghlan: for syntax [6]
(f(a) + o(b) - o(c) for (a, b, c))
(x * x for (x))
(x for ())
(x.bar(*a, **k) for (*a, **k))
((x(*a, **k) for ()) for x, a, k in funcs_and_args_list)

Nick Coghlan: def-from syntax [4]
(def f(a) + o(b) - o(c) from (a, b, c))
(def x * x from (x))
(def x from ())
(def x.bar(*a, **k) from (*a, **k))
((def x(*a, **k) from ()) for x, a, k in funcs_and_args_list)

Michael Spencer: from-args syntax [5]
(f(a) + o(b) - o(c) from args(a, b, c))
(x * x from args(x))
(x from args())
(x.bar(*a, **k) from args(*a, **k))
((x(*a, **k) from args()) for x, a, k in funcs_and_args_list)

Michael Spencer: for-args syntax [5]
(f(a) + o(b) - o(c) for args(a, b, c))
(x * x for args(x))
(x for args())
(x.bar(*a, **k) for args(*a, **k))
((x(*a, **k) for args()) for x, a, k in funcs_and_args_list)


So there's a bunch of ideas out there.  I don't know if any of them 
could be overwhelmingly preferred over lambda.

Personally, I lean slightly towards the def-from syntax because it uses 
the 'def' keyword to bring your attention to the fact that a function is 
being defined, and it gives the expression precedence over the arglist, 
which makes sense to me for an anonymous function, where (IMHO) the 
expression is really the most important part of the declaration.

OTOH, I think Michael Spencer's args() function, if implementable, could 
have a lot of cool uses, like getting the arguments passed to next 
within a generator.  (See the thread about that[8].)


Steve

[1]http://mail.python.org/pipermail/python-list/2004-December/256859.html
[2]http://mail.python.org/pipermail/python-list/2004-December/256881.html
[3]http://mail.python.org/pipermail/python-list/2004-December/257023.html
[4]http://boredomandlaziness.skystorm.net/2004/12/anonymous-functions-in-python.html
[5]http://mail.python.org/pipermail/python-list/2004-December/257893.html
[6]http://mail.python.org/pipermail/python-list/2004-December/257977.html
[7]http://mail.python.org/pipermail/python-list/2005-January/258441.html
[8]http://mail.python.org/pipermail/python-list/2005-January/258238.html



More information about the Python-list mailing list