Returning values from a lambda

John McMonagle johnmc at velseis.com.au
Fri Nov 16 03:07:05 CET 2001


William Tanksley wrote:

> On Fri, 16 Nov 2001 10:36:06 +1000 (EST), John McMonagle wrote:
> 
>>I wish to return values from a lambda function which is bound to the
>>Tkinter button command option.  For example,
>>
> 
> Wait a second.  You don't want to return values; you want to modify
> variables.  There's a HUGE difference; lambdas can do the first, but they
> don't support any of the syntax to do the second.
> 
> The solution is to not use a lambda.  Just define a function which
> modifies the variables you need, and pass that function's name in place of
> the lambda.


But I need to use a lambda in order to pass arguments to the function.


> 
> 
>>def operations(a,b):
>> return a+b, a*b
>>
> 
> def operations(a,b):
>    x = a+b
>    y = a*b
> 


Indeed I could do it like that, but I would need to define globals for x 
and y.  Can this be done without using globals ?

>>from Tkinter import *
> 
>>root = Tk()
>>btn = Button(root, text='Press Me', command=lambda a=2, b=5: operations(a,b))
>>btn.pack()
>>
> 
> With the function I've defined, things should work.
> 
> 
>>John
>>
> 






More information about the Python-list mailing list