Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.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; 'modify': 0.07; 'python3': 0.07; 'variables': 0.07; '22,': 0.09; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '(1,': 0.16; 'closures': 0.16; 'different?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ought': 0.16; 'subject: \n ': 0.16; 'two,': 0.16; 'sat,': 0.16; 'weird': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'variable': 0.18; '>>>': 0.22; 'saying': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'dec': 0.30; 'important.': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '>>>>': 0.31; 'problem': 0.35; "can't": 0.35; 'done.': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'doing': 0.36; 'useful': 0.36; 'should': 0.36; 'pm,': 0.38; 'does': 0.39; 'sure': 0.39; 'how': 0.40; "you're": 0.61; 'first': 0.61; 'here': 0.66; 'between': 0.67; 'mar': 0.68; 'behavior': 0.77; 'subject:this': 0.83; 'to:none': 0.92; 'inc,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=wbqoeFhsd41Ap1QznDe0A/xB6+aBxNkqz1NMhunnBXc=; b=k+yVncwoy45eZQkCIKFryEg0drJxSRsw913AjG3rxZyC9mBnYF1jVC6jcARy8MHT0A 0fYPQGPV9HNvP/Aq9PPyOgeLnPv/LXk06R6MSDXTL5INVeATLnI3qRvgRuS06r0KPTo2 p8zjUqPmSHaR77qjkPISQoYVmy9Rzg6YbdrQKtzrCcv2/P9vzzJ2ERiTjPCmW5Sr4b1L hJ71Zw1gcxwdBV1GhCvGMGxJ1KvOWmNoVH0zxDd3nWnmBNR1iEMDPvw+RjcuCIgo6nQZ EYPIJZ3stZekz2as8ceAP8iwHkttQ+yoO/A58UnQH5vT6O+nzmmlhIZrMdbCrK4EC6Lb Vc+g== MIME-Version: 1.0 X-Received: by 10.68.197.36 with SMTP id ir4mr56510270pbc.46.1395456087219; Fri, 21 Mar 2014 19:41:27 -0700 (PDT) In-Reply-To: References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> Date: Sat, 22 Mar 2014 13:41:27 +1100 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395456097 news.xs4all.nl 2848 [2001:888:2000:d::a6]:59529 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68733 On Sat, Mar 22, 2014 at 1:06 PM, Rustom Mody wrote: > Two: A comprehension variable is not bound but reassigned across the > comprehension. This problem remains in python3 and causes weird behavior when > lambdas are put in a comprehension > >>>> fl = [lambda y : x+y for x in [1,2,3]] >>>> [fl[i](2) for i in [0,1,2]] > [5, 5, 5] To clarify, what you're saying here is that x in the first comprehension's closures should be bound to separate values for x, yes? I'm not sure how that ought to be done. Having closures that can reference and modify each other's variables is important. def func_pair(): x = 0 def inc(): nonlocal x; x+=1 return x def dec(): nonlocal x; x-=1 return x return inc, dec fooup, foodn = func_pair() barup, bardn = func_pair() >>> fooup(), fooup(), fooup(), foodn() (1, 2, 3, 2) >>> barup(), barup(), bardn(), bardn() (1, 2, 1, 0) Those functions are fundamentally linked. Very useful with callbacks. A nice alternative to doing everything with bound methods. So if that's not going to be broken, how is this fundamentally different? def func_loop(): for x in 1,2,3: yield (lambda: x) one, two, three = func_loop() one(), one(), two(), two(), three(), three() This one does NOT work the way the names imply, and I can see that you'd like to fix it. But I can't pinpoint a significant difference between them. How do you distinguish? ChrisA