Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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; 'function,': 0.09; 'guys.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'thats': 0.09; 'def': 0.12; 'creates': 0.14; '"current': 0.16; "'a',": 0.16; '(at)': 0.16; 'arguments:': 0.16; 'called,': 0.16; 'called.': 0.16; 'enough.': 0.16; 'expects': 0.16; 'finney': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'positional': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'syntax,': 0.16; 'typeerror:': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'passing': 0.19; 'widget': 0.19; 'select': 0.22; 'import': 0.22; 'header :User-Agent:1': 0.23; 'error': 0.23; '31,': 0.24; 'looks': 0.24; 'equivalent': 0.26; 'this:': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'fixed': 0.29; "doesn't": 0.30; "i'm": 0.30; 'writes:': 0.31; 'file': 0.32; 'selection': 0.32; 'option': 0.32; 'another': 0.32; 'skip:u 20': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'skip:" 50': 0.36; 'var': 0.36; 'material': 0.36; 'should': 0.36; 'error.': 0.37; 'two': 0.37; 'being': 0.38; 'button': 0.38; 'ben': 0.38; 'gmail': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'read': 0.60; 'expression': 0.60; 'radio': 0.60; 'new': 0.61; 'reach': 0.63; 'here': 0.66; 'carefully': 0.74; 'experiment': 0.84; "it'd": 0.84; 'received:myvzw.com': 0.84; '\xe2\x80\x9cthe': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: memilanuk Subject: Re: Question on lambdas Date: Mon, 08 Dec 2014 23:15:55 -0800 References: <85oarditil.fsf@benfinney.id.au> <85fvcpie5j.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 62.sub-70-199-132.myvzw.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: <85fvcpie5j.fsf@benfinney.id.au> 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: 93 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418109369 news.xs4all.nl 2908 [2001:888:2000:d::a6]:51556 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82184 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. 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 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