[Tutor] Why use lambda?
bob gailer
bgailer at gmail.com
Sat Aug 2 22:17:51 CEST 2008
desmond mansfield wrote:
> I've read some small tutorials on lambda, and how I can use it to define functions "on-the-fly".
> But what I don't understand, and cannot seem to find an answer to, is why I'd actually want to use it ?
>
> What can lambda do that normal function definitions cannot?
> Is it quicker to execute/less memory intensive?
> Or is it just quicker to type and easier to think about?
In my Python Pipelines program for the Count stage I have:
opts = {
'characters' : lambda rec, tot: tot + len(rec),
'words' : lambda rec, tot: tot + len(rec.split()),
'lines' : lambda rec, tot: tot + 1,
'minline' : lambda rec, tot: min(len(rec), tot),
'maxline' : lambda rec, tot: max(len(rec), tot),
}
Consider how many more lines of code it would take if I had to use defs.
Consider how readable it is to have the expressions all in one place.
--
Bob Gailer
919-636-4239 Chapel Hill, NC
More information about the Tutor
mailing list