Python Tutorial Was: Guido's regrets: filter and map

sismex01 at hebmex.com sismex01 at hebmex.com
Tue Nov 26 09:54:38 EST 2002


> From: Grant Edwards [mailto:grante at visi.com]
> Sent: Monday, November 25, 2002 10:27 PM
> 
> I find lambda quite useful for creating simple anonymous 
> functions to pass to GUIs as button commands and the like.
> I suppose the same effect could be obtained by adding a couple
> more lines to define a temporarily-named function, but then I'd
> have to think up a name.  I've been known to waste hours trying
> to come up with names for new computers. I'm not quite so
> indecisive about functions, but it does seem to be one of my 
> weak points.
>

Ahh!!

But the ideal throw-away name for anonymous functions
is "_", or "__".  I always use it for one-line definitions
because it's clear, and ... "forgetable". :-)


>>> def _(): print >> sys.stderr, "Button clicked!"
>>> btn = Tkinter.Button(main, command=_)


Since "_"'s usage is exactly on the next line from it's
definition, you know where it went, and you can forget
about it without remorse, or reuse it.  Sure, it has
the "problem" of being two lines instead of one, and
a lambda in that position is more concise:


>>> btn = Tkinter.Button(main, command=lambda:sys.stderr("Button
clicked!\n"))


but, with a lambda, you are limited to only an expression,
not any statements.  So using a throwaway function does
have it's advantages.


Anyway, to each it's own.  But I do concurr with those who
say that with the acquisition of list comprehensions, map(),
reduce(), filter(), lambda() et-al, should be on the road
to deprecation and eventual elimination, being the black
un-pythonic sheep of the family.

Good Tuesday!

-gustavo




More information about the Python-list mailing list