Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone05.am1.xlned.com!bcyclone05.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parameters': 0.04; 'syntax': 0.04; 'root': 0.05; 'subject:Question': 0.07; 'tkinter': 0.07; 'arguments': 0.09; 'arguments,': 0.09; 'function,': 0.09; 'thats': 0.09; 'def': 0.12; 'creates': 0.14; '"current': 0.16; "'a',": 0.16; 'called,': 0.16; 'called.': 0.16; 'enough.': 0.16; 'expects': 0.16; 'finney': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'sense,': 0.16; 'specifying': 0.16; 'syntax,': 0.16; 'values),': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'passing': 0.19; 'version.': 0.19; 'widget': 0.19; 'command': 0.22; '>>>': 0.22; 'select': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'certainly': 0.24; 'stick': 0.24; 'looks': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'point': 0.28; 'function': 0.29; 'fixed': 0.29; 'am,': 0.29; "doesn't": 0.30; "i'm": 0.30; 'context,': 0.31; 'ordinary': 0.31; 'writes:': 0.31; 'selection': 0.32; 'option': 0.32; 'another': 0.32; 'used,': 0.33; 'skip:u 20': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'var': 0.36; 'words,': 0.36; 'material': 0.36; 'error.': 0.37; 'two': 0.37; 'being': 0.38; 'button': 0.38; 'ben': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'read': 0.60; 'expression': 0.60; 'radio': 0.60; 'new': 0.61; "you're": 0.61; 'such': 0.63; 'here': 0.66; 'received:74.208': 0.68; 'default': 0.69; 'carefully': 0.74; 'experiment': 0.84; "it'd": 0.84; '\xe2\x80\x9cthe': 0.91 Date: Tue, 09 Dec 2014 03:03:16 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Question on lambdas References: <85oarditil.fsf@benfinney.id.au> <85fvcpie5j.fsf@benfinney.id.au> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:8PKzvO0gnXy26PbJ1u9XUMp3vo3PVb9TXtJlF5LgGiR G6og+DnjBYglut5OEIg17LhvMuWnJA0NM3U49dnx/0FQUPPuAT EZJ5xnSSACxcfqE2uFTnYeWsArPKgP9txW2esxgDKIPSeg5lS6 48hzI21DWzbQDXGm7iRgN5DBIosX4m1WyVNpjLElMNVLn5rDR3 gL/RszUlhFFxhXOc1usKCPmCqm1eL42kHkVDZhK0gLHer1l24Z runly793Obkcgwfu+IlyexAOcjCXdbLuNE+Ad/eCd836Xth+as NOa5N0CQomRsbyJBy2d/meMzvG0eWdrXSTfgPpYjKYkCTjmXVd DDi03jkaZCPiIvdwNY3U= X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 123 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418112210 news.xs4all.nl 2922 [2001:888:2000:d::a6]:35018 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 913670856 X-Received-Bytes: 7855 Xref: csiph.com comp.lang.python:82188 On 12/09/2014 02:15 AM, memilanuk wrote: > On 12/08/2014 09:30 PM, Ben Finney wrote: >> memilanuk 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