Let's Talk About Lambda Functions!

Andrae Muys amuys at shortech.com.au
Sun Jul 28 20:47:56 EDT 2002


Carl Banks <imbosol at vt.edu> wrote in message news:<ahsmda$pjm$2 at solaris.cc.vt.edu>...
> Second, if you're going to do it right often, you might want to wrap
> it up in a function like this:
> 
>     def sort_the_right_way (f, a):
>         aux = [ (f(x), x) for x in a ]
>         aux.sort()
>         a[:] = [ x for __, x in aux ]
> 
> In which case, the temptation to use lambda returns:
> 
>     sort_the_right_way (lambda x:x[1], a)

Indeed.  I personally only find lambda helpful when I require a truely
trivial function, swapping the order of a pair: lambda (x,y):(y,x); 
applying the second argument: lambda x,f:f(x); etc.

Andrae Muys



More information about the Python-list mailing list