anonymous functions/expressions without lambda?

Dave Benjamin ramen at lackingtalent.com
Thu Apr 28 16:09:03 EDT 2005


Cameron Laird wrote:
> In article <aeGdnR0QWZ8XYu3fRVn-vg at powergate.ca>,
> Peter Hansen  <peter at engcorp.com> wrote:
> 
>>This meets your requirements as stated:
>>
>>def temp():
>>   foo.var = 1
>>
>>bind('a', temp)
>>
>>def temp():
>>   foo.var = 2
>>
>>bind('b', temp)
>>
>>del temp
> 
> Ewww!  *When* is lambda going bye-bye?  I apparently
> haven't been paying close enough attention.  Among other
> considerations, I still instruct people to use lambda for
> plenty of specific cases.

Well, IMNSHO, it's an empty threat that's been looming on the horizon 
for several years now. ;) Most recently, Guido mentioned his desire to 
remove the keyword in his post on Artima, which resulted in a huge debate:

http://www.artima.com/weblogs/viewpost.jsp?thread=98196

Note that in the OP's question, as with Peter's example above, you still 
can't do it with lambda alone, since you can't mix expressions with 
assignment. Nonetheless, lambda is nice for delaying binding, and 
combined with a helper function that sets attributes, it can produce the 
most concise solution:

bind('a', lambda: setattr(foo, 'var', 1))
bind('b', lambda: setattr(foo, 'var', 2))

The usual response is that you probably want to make a class anyway, and 
use bound methods for this type of stuff. But I find that in GUI/async 
programming, often you need a little dab of glue to connect events 
between objects, and lambda provides a nice way to avoid 
over-abstracting the problem.

We'll see how this all pans out in the next few years. I'm not too fond 
of removing features from a language for purely aesthetic reasons, but 
lambda's really stuck in a syntactic quandry due to the 
statement/expression dichotomy, so I can understand to some extent 
Guido's desire to remove it.

Dave



More information about the Python-list mailing list