Fate of lambda, Functional Programming in Python...
Jeff Sandys
sandysj at juno.com
Fri Aug 20 16:26:15 EDT 2004
"Christopher A. Craig" wrote:
>
> "Jared Cohen" <Jared.Cohen at noaa.gov> writes:
>
> > Agreed! I'm currently building a GUI in Tkinter, with event
> > handlers and callbacks all over the place. The lambda form is an
> > absolute MUST for this kind of thing!
>
> Nobody is talking about removing the ability to pass functions, just
> the actual "lambda" form.
>
> You can still build a GUI by doing
>
> def spam(stuff):
> def eggs(parrot):
> return 5+parrot
> return eggs
>
> You just can't do
>
> def spam(stuff):
> return lambda foo: 5+parrot
>
> I can't think of a situation where you _really_ need the second
> (and much more limited) form.
How about this use of lambda?
(from another post 'RE: How to sort this kind of list easily?' today)
| > > Hi,all
| > > I have a list like [(id,string),...],for example:
| > >
| > > [(1,'xxxxx'),(7,'ppppp'),(4,'gggggg'),...]
| > >
| > > I want to sort this list according to the id of each element.
| > > After sorting,the list will become:
| > >
| > > [(1,'xxxxx'),(4,'gggggg'),(7,'ppppp')...]
| >
| > list.sort sorts tuples by first argument, then second &c. If you
want
| > a custom sort order, look at the decorate-sort-undecorate pattern.
|
| Or if you want to sort only on your Id, use a lambda:
| l.sort(lambda x,y: cmp(x[0],y[0]))
|
| To force sorting on just the nth element of the tuples, replace 0
with
| n in the above.
|
I think that lambda should be unlimited and expanded. It already has
the
colon, block delimiter, so allow lambda to be a multistatement and
multiline
indented block!
Thanks,
Jeff Sandys
More information about the Python-list
mailing list