Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.032 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'function,': 0.07; 'used.': 0.07; 'callback': 0.09; 'func': 0.09; 'def': 0.10; 'called,': 0.16; 'did,': 0.16; 'eckhardt': 0.16; 'funcs': 0.16; 'lambda': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'code.': 0.20; 'anonymous': 0.22; 'work,': 0.22; "haven't": 0.23; 'least': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; "i'm": 0.29; 'function': 0.30; 'print': 0.32; 'defining': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'doing': 0.35; 'really': 0.36; 'but': 0.36; 'does': 0.37; 'two': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'step': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'back': 0.62; 'series': 0.63; 'different': 0.63; 'obvious': 0.71; 'received:74.208': 0.71; 'discover': 0.72; 'greetings!': 0.84; 'local,': 0.84; 'received:74.208.4.194': 0.84 Date: Wed, 06 Feb 2013 09:37:07 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: puzzled by name binding in local function References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:ouOcgZAAb3abNejsn+GT6Na2Zigy7PXGvPdKqflm1zd o7pZ883DQ8JR2sZxcR+qofppCvguQ+FoLUN9CNz3UbTbd8n7ar kbU0qLdcnuNmn7JLV4GYz4BcLg6lcd30GnCDBSbUMgUivQ1NfW IAAPWsXFUbBs9t+fYqZnnbZY+PksGcWwWrEjcmyZLvvQyyQAki QE+DRudVV6BKg73rvgfRej6+guVxEQGUF61HgKDRj7neGG2CD+ ZH6qKQAtLx6hC3C46rb14Qrr6PVRcXJwLpbj07ZHFv/q2GjpbD 5epNQup2wx3cWGipYenQRwR9BYnAWqSDrXPjYVRdqhXU9SFBA= = 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360161459 news.xs4all.nl 6939 [2001:888:2000:d::a6]:50669 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38279 On 02/06/2013 05:19 AM, Ulrich Eckhardt wrote: > Dave and Terry, > > Thanks you both for your explanations! I really appreciate the time you > took. > > Am 05.02.2013 19:07, schrieb Dave Angel: >> > >> The main place where I see this type of problem is in a gui, where >> you're defining a callback to be used by a series of widgets, but you >> have a value that IS different for each item in the series. You write a >> loop much like you did, and discover that the last loop value is the >> only one used. The two cures above work, and you can also use lambda >> creatively. > > Careful, lambda does not work, at least not easily! The problem is that > lambda only creates a local, anonymous function, but any names used > inside this function will only be evaluated when the function is called, > so I'm back at step 1, just with even less obvious code. > > > Greetings! > > Uli > > I haven't been able to reconstruct it exactly, but here's the function equivalent: def myfunc2(i): def myfunc2b(): print ("myfunc2 is using", i) return myfunc2b funcs = [] for i in range(5): funcs.append(myfunc2(i)) print "Now doing the loop2" for func in funcs: func() -- DaveA