what's in a name (was Re: using lambda to print everything in a list)

James_Althoff at i2.com James_Althoff at i2.com
Mon Apr 30 15:47:16 EDT 2001


>Alex writes:
>
>_In the context of Python_ (as opposed to, say, Haskell), I don't
>find myself "composing functions" with wild abandon.  I do pass
>functions to other functions, etc, but in recent times I find
>myself using named local functions instead of unnamed lambdas for
>that, more and more -- lately, I would say, exclusively.  And I
>don't miss the lambdas at all...
>
>
>Alex

Just to (re)state one case for the minority -- apparently 3 of us out of
the approximately 17 million or so Python programmers -- who do miss the
"fully functional, fully integrated, and conveniently "syntaxed" lambda
that never existed in Python, I still would like to see (dream, dream,
dream) an alternative way of specifying a code block for the following kind
of thing -- which I often encounter in code in my neck of the woods:

savedCursor = window.getCurrentCursor()
window.showWaitCursor()
try:
    line1-of-code
    line2-of-code
    . . .
    lineN-of-code
finally:
    window.showCursor(savedCursor)

Because I think:

def doSomeLinesOfCode():
    line1-of-code
    line2-of-code
    . . .
    lineN-of-code
window.showWaitCursorDuring(doSomeLinesOfCode)

is really a preferred idiom.  In practice, I don't see folks around my
block do the latter much, although there is nothing in today's Python that
prevents it.  I do believe I would see this idiom used much more frequently
(in my building anyway) were there something akin to:

window.showWaitCursorDuring:
    line1-of-code
    line2-of-code
    . . .
    lineN-of-code

By comparison, one does see this idiom used frequently in Smalltalk (and
apparently from a recent post, in Ruby as well).  I suspect that there is
something in the combination of 1) pick a temp-code-block name, 2)
construct the nested function _before_ its usage (i.e., use a "prefix"
ordering where many folks are used to a "postfix" ordering -- due, more
likely than not, to the old-worn-in-shoes-type comfort of for loops and
conditional statements), and 3) use a nested function definition while you
are at it that provides just enough straws to send serious soreness to the
camel's back (in spite of how admittedly easy it is to name a code block in
current Python).

jim





More information about the Python-list mailing list