Question on lambdas

Dave Angel davea at davea.name
Tue Dec 9 03:03:16 EST 2014


On 12/09/2014 02:15 AM, memilanuk wrote:
> On 12/08/2014 09:30 PM, Ben Finney wrote:
>> memilanuk <memilanuk at gmail.com> writes:
>>
>>> ...
>>> lambda: update_label2('A', 100)
>>>
>>> would this work the same?
>>
>> (I don't know what you mean here by “the same”; the same as what?)
>>
>> The above creates a new function, which expects no parameters (because
>> there are no parameters before its ‘:’).
>>
>> The function, when called, will return the value of the expression
>> ‘update_label2('A', 100)’.
>>
>>> It looks as though it'd be passing the same two parameters to the same
>>> function...
>>
>> Yes, it looks that way, and that's what it does. The parameters are
>> fixed in the expression and will be the same each time the new function
>> is called.
>>
>>> lambda: 'A', 100: update_label2()
>>
>> This is a syntax error.
>>
>> Have you tried experimenting with these? Carefully read the reference
>> material on the ‘lambda’ syntax, and create some functions and
>> experiment with them.
>>
>
> Fair enough.  What I was trying to do when I got into all this was
> something like this:
>
> import tkinter as tk
>
> root = tk.Tk()
> root.wm_title('Radio Buttons')
>
> label1 = tk.Label(root)
> label1.config(text='Please select an option below.')
> label1.pack()
>
>
> def update_label2(option, value):
>      selection = "current selection is: \n\n" + str(option) + ": " +
> str(value)
>      label2.config(text=selection)
>
> var = tk.IntVar()
> var.set(100)
>
> R1 = tk.Radiobutton(root, text='A', value=100, variable=var,
>                      command=lambda: update_label2('A', 100))
> R1.pack(anchor='center')
> R2 = tk.Radiobutton(root, text='B', value=80, variable=var,
>                      command=lambda option='B', value=80: update_label2())
> R2.pack(anchor='center')
> R3 = tk.Radiobutton(root, text='C', value=60, variable=var,
>                      command=lambda: update_label2('C', 60))
> R3.pack(anchor='center')
>
>
> label2 = tk.Label(root)
> label2.config(text='Current selection is: \n \nA: 100')
> label2.pack()
>
> root.mainloop()
>
>
> So to me, the purpose of 'lambda' in this case is to call a function
> when a particular widget - in this case a radio button - is clicked.
> Thats why I wondered if 'lambda: update_label2('A', 100)' - where I'm
> using lambda to call another function, with the parameters inside the
> parentheses, was equivalent to 'lambda 'A', 100: update_label2()' where
> I would be using lambda to 'feed' the parameters to the function.

You can certainly use lambda to "feed" the values, but you're calling 
the update_label function with no arguments.  If you always want to pass 
those particular values, stick to the
    command=lambda: update_label2('A', 100))

version.  If the values have to be calculated, then you might want
     command=lambda: update_label2('A', 100*SOME_GLOBAL))

But specifying arguments to the lambda that you don't use makes no sense.

     command=lambda option='B', value=80: update_label2())

The locals option and value aren't being used, so they're just wasted 
characters.  In another context, something like:

     command=lambda option='B', value=80: update_label2(option, 2*value))

might make sense, but since tkinter isn't going to pass any arguments to 
the function, there's no point in declaring them.

Try writing ordinary functions for your command= to use, and experiment 
with those.

def mycallback():
     update_label2('A', 100)

       command = mycallback

Notice that tkinter will not be passing any arguments, so there's no 
point in having any parameters in mycallback().  But also notice that if 
you do declare such parameters (with default values), those values won't 
be automatically passed to the update_label() function.

In other words, this:

def mycallback(a = 5, b = 12):
     update_label2()

doesn't automagically pass a and b into update_label2.



-- 
DaveA



More information about the Python-list mailing list