Let's Talk About Lambda Functions!
Alex Martelli
aleax at aleax.it
Fri Jul 26 17:26:23 EDT 2002
More information about the Python-list mailing list
Fri Jul 26 17:26:23 EDT 2002
- Previous message (by thread): Let's Talk About Lambda Functions!
- Next message (by thread): Let's Talk About Lambda Functions!
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Paul Rubin wrote: > "Britt A. Green" <python at experimentzero.org> writes: >> So I know what lambda functions are, they're syntax and how they're used. >> However I'm not sure *why* one would use a lambda function. What's the >> advantage that they offer over a regular function? > > You can use them inside other expressions, e.g. suppose you want to > sort a bunch of strings starting with the second letter: > > a = ['apple', 'banana', 'pear', 'watermelon'] > a.sort(lambda x,y: cmp(x[1],y[1])) > > => ['banana', 'watermelon', 'pear', 'apple'] > > Without lambda, you'd have to define a separate named function: Without lambda, you might be more tempted to do it right (a la DSU): aux = [ (x[1], x) for x in a ] aux.sort() a[:] = [ x for __, x in aux ] When you're sorting 4 items, it doesn't matter, but try -- not a huge number -- just 400, say. > There's a middle ground that says the name "lambda" (which comes from > Lisp) wasn't a good choice and another name should have been used > instead, like maybe 'func', which does the same thing. I guess that's > reasonable. I'm used to lambda from Lisp but another name would > probably be less scary to non-Lisp-users. It's not an issue of syntax sugar as superficial as what keyword to use. A REAL lambda, by any other name -- the ability to package up ANY chunk of code, not just an expression -- might add power enough to justify its existence. Today's lambda's just deadweight. And I do _not_ say that as a "non-Lisp-user" (though it's been a LONG time since I last used Lisp, or Scheme for that matter, in production use). Alex
- Previous message (by thread): Let's Talk About Lambda Functions!
- Next message (by thread): Let's Talk About Lambda Functions!
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list