anonymous functions/expressions without lambda?
Cameron Laird
claird at lairds.us
Thu Apr 28 15:08:03 EDT 2005
In article <aeGdnR0QWZ8XYu3fRVn-vg at powergate.ca>,
Peter Hansen <peter at engcorp.com> wrote:
>Paul Miller wrote:
>> For example, let's say I have a function which binds a key to a function
>> call. I want to do something "simple" in this function call, and I have
>> a lot of bindings, so I don't want to have a ton of tiny little
>> functions scattered around:
>>
>> def setVarTo1():
>> foo.var = 1
>> def setVarTo2():
>> foo.var = 2
>>
>> bind('a', setVarTo1)
>> bind('b', setVarTo2)
>>
>> Instead, I'd like to do something like this:
>>
>> bind('a', foo.var = 1)
>> bind('b', foo.var = 2)
>>
>> What's the recommended way to do something like this?
>
>This meets your requirements as stated:
>
>def temp():
> foo.var = 1
>
>bind('a', temp)
>
>def temp():
> foo.var = 2
>
>bind('b', temp)
>
>del temp
>
>
>-Peter
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.
More information about the Python-list
mailing list