Question about Tkinter

Grant Edwards grante at visi.com
Sat Aug 11 05:51:11 CEST 2001


On Fri, 10 Aug 2001 22:20:08 +0200, Alex Martelli <aleaxit at yahoo.com> wrote:
>    ...
>> > Use different callables for the callbacks -- if it must be
>    ...
>> A slightly different way is to use lamgda.  It's best for
>> simple cases where you just want to pass a few arguments that
>
>I disagree with your claim that it's best.

My phrasing was bad.  What I meant was that of the various
situations where one might use lambda the simplest cases are
the best candidates for lambda usage.  I wasn't claiming that
lambda is the best option compared to curry or local defs.


>>  15    b.bind("<Button-1>",lambda event,xx=x,yy=y: board.toggleSpace(xx,yy))
>
>I'd code this (assuming no nested scope, just as you're
>assuming):
>
>         def togglethis(event, xx=x,yy=y):
>             board.toggleSpace(xx,yy)
>         b.bind("<Button-1>", togglethis)

That's definitely better.  For some reason (undoubtedly due to
restrictions in other languages) I often forget that I can use
a "def" inside the body of a function.

In this case, it's probably a lambda because this program was
translated from Scheme, and it was already a lambda. :)

>Same for my favourite solution, except that there is no lambda
>form -- the local def takes lambda's place.
>
>> Curry is probably a more readable solution to a non-Schemer.
>> ;)
>
>I am or used to be enough of a schemer that our common-lisp
>friend recently caught me out as an adorer of tail recursion,

Yup, I was recentlry caught with my Scheme history showing when I
assumed that recursion was a preferred flow-control mechanism
in CL.

>but I still dislike _Python's_ lambda, with all of its limits
>and issues.

It's obscure _and_ crippled.  One or the other could be
tolerated.  I've been tripped up by lambda's scoping issues
more than once (but local def's have the same problem -- at
least for now).

>A local def appears to me to be a preferable solution in just
>about every case.

I can't really come up with an example where a lamba is
superior to a local def for anything that's not completely
trivial.  The only place a lamba seems like a good option is
applying a trivial function using map() or filter(). But, that
usage has been made largely redundant with list comprehensions.

But -- since there's no such thing as tuple-comprehensions,
you still need filter() if you're working with tuples, so maybe
lambda still has some utility is situations like:

 t2 = filter(lambda x: x>0, t1)

On a completely different topic, I don't quite understand why,
when passed a tuple, map() returns a list and filter() returns
a tuple.  I was quite surprised that they do different things.
 
-- 
Grant Edwards                   grante             Yow!  NEWARK has been
                                  at               REZONED!! DES MOINES has
                               visi.com            been REZONED!!



More information about the Python-list mailing list