Question on lambdas

memilanuk memilanuk at gmail.com
Tue Dec 9 02:15:55 EST 2014


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.  I see 
what you mean about the syntax error = it should be 'lambda option='A', 
value=100:update_label2()' but even then it doesn't really work as the 
update_label2 function complains about not being given any values at all.

   File "/home/monte/Dropbox/Code/python/sandbox/reddit.py", line 31, in 
<lambda>
     command=lambda option='B', value=80: update_label2())
TypeError: update_label2() missing 2 required positional arguments: 
'option' and 'value'


-- 
Shiny!  Let's be bad guys.

Reach me @ memilanuk (at) gmail dot com




More information about the Python-list mailing list