Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1a.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'expressions': 0.07; '*is*': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:language': 0.09; 'def': 0.12; 'jan': 0.12; 'arg': 0.16; 'called.': 0.16; 'closure,': 0.16; 'created.': 0.16; 'expressions.': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'bit': 0.19; 'examples': 0.20; 'seems': 0.21; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'subject:list': 0.30; 'once,': 0.31; 'another': 0.32; 'call.': 0.33; 'problem': 0.35; 'definition': 0.35; 'there': 0.35; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'expression': 0.60; 'hope': 0.61; 'received:173': 0.61; 'course': 0.61; 'making': 0.63; 'such': 0.63; 'within': 0.65; 'default': 0.69; 'funny': 0.74; 'subject:this': 0.83; 'grabbing': 0.84; 'received:fios.verizon.net': 0.84; 'remember,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) Date: Mon, 24 Mar 2014 21:20:48 -0400 References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <532d5bd9$0$29994$c3e8da3$5496439d@news.astraweb.com> <87ior3w740.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395710470 news.xs4all.nl 2830 [2001:888:2000:d::a6]:49323 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68925 On 3/24/2014 7:56 PM, Mark H Harris wrote: > Of course the problem is that the closure A function is not a closure unless defined within another function. In the examples I remember, there was no nesting. > grabs the *last* number in > the list which is used for each of the adder[] functions created. Wrong. Functions look up global and nonlocal names, such as n, when the function is called. >>> adders = list(range(4)) >>> for n in adders: adders[n] = lambda a: a+n >>> n = 1000 >>> adders[1](3) 1003 Same result if the function *is* a closure, making n nonlocal rather than global. def f(): adders = list(range(4)) for n in adders: adders[n] = lambda a: a+n n = 1000 return adders print(f()[1](3)) >>> 1003 The only definition time grabbing is with default arg expressions. This discussion is a bit funny in a way. Some people are puzzled that default arg expressions 'grab' just once, as definition time, rather than with each call. Others (I hope others) are puzzled that body expressions 'grab' with each call, rather than just once, at definition time. That seems to be particularly true when the body is in a lambda expression rather than a def statement. -- Terry Jan Reedy