I sing the praises of lambda, my friend and savior!

Clark C. Evans cce at clarkevans.com
Wed Oct 13 21:09:20 CEST 2004


On Wed, Oct 13, 2004 at 10:47:56AM -0700, Jeff Shannon wrote:
| Except that the Python philosophy is that overall, the greatest clarity 
| is achieved by having a single obvious way to do things.

When doing coding that uses alot of callbacks, use of lambda for
tiny, nameless, single-use expressions is the "single obvious way to
do something".

Assume for a moment the following programming costs:

  2m  Minting a name, that is, finding a name that is not 'foo'
      which semantically adds to the readability and maintainability
      of the code.
        
  30s Verifying that a function name is not used more than once to 
      verify that changing its definition, or if it is reused, 
      making sure all the places where a change would affect is OK.

In one of my files, I've got 20 or so lambdas, typically only a few
characters to plug-in a computation.  You're asking me to pay a 40
minute price just to mint names in this file?  Then, I've had, over
the past year about 20 edits on that file, some of which change a
few of these lambdas, say 2 changes per edit on average.  That's
another 20 minutes.  So, over the last year, in this one file alone,
not having lambdas would have cost me an additional hour of time!
I've got 2 largish projects, each averaging about 20 files, so
that's about 40 hours of time chewed up with small efficiency hits.
Or, about $4,000 of billable time.   I'd rather keep the money or
take a vacation, thank you very much.

I use Python beacuse it is a horribly efficient language for me to
write software with; it is efficient beacuse I have the flexibiltiy
to use several different constructs depending on which is more
clearly expresses the intent of the developer. While lambda is a
small part of this effiency, it is a factor.  It clearly expresses a
simple, nameless single chunk of code that is never reused.

Propose another construct that has these features, and I'll gladly
use it instead of lambda.

Best,

Clark



More information about the Python-list mailing list