lambda and for that matter goto not forgetting sugar

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Feb 11 05:53:30 EST 2005


Nick Coghlan wrote:
> Anyway, check out AlternateLambdaSyntax on the python.org Wiki

It's an interesting page:
http://www.python.org/moin/AlternateLambdaSyntax
Some days ago I suggested something different: maybe def can become a
function, then it can work as lambda (and still as the old def) too.
Something like:

original :
lambda a, b, c:f(a) + o(b) - o(c)
lambda x: x * x
lambda : x
lambda *a, **k: x.bar(*a, **k)

==>

def(a, b, c): return f(a) + o(b) - o(c)
fun1 = def(x): return x * x
def(): return x
def(*a, **k): return x.bar(*a, **k)

Such def can be used as the lambda inside expressions too.

fun1 = def(a, b, c): return f(a) + o(b) - o(c)
This is the same as the old:
def fun1(a, b, c):
  return f(a) + o(b) - o(c)

(That use of "return" in the middle is a little verbose to use it for
functions inside expressions.)

That Ruby syntax at the end of page is cute.
{a,b,c | return f(a) + o(b) - o(c)}
{a,b,c | f(a) + o(b) - o(c)}
def(a, b, c | f(a) + o(b) - o(c) )
fun(a, b, c | f(a) + o(b) - o(c) )
(etc. there are lots of other possibilities).

If def becomes a function like that, then beside this syntax:
fun1 = def(a, b, c): return f(a) + o(b) - o(c)
This other one can be interesting for expressions only:
def(a, b, c | f(a) + o(b) - o(c) )
(Both can be acceptable at the same time).

Bye,
Bearophile




More information about the Python-list mailing list