Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'subject:Python': 0.06; 'variables': 0.07; '22,': 0.09; 'statements': 0.09; 'subject:language': 0.09; 'thats': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'language,': 0.12; 'declared': 0.16; 'different?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration': 0.16; 'merely': 0.16; 'scope,': 0.16; 'sense,': 0.16; 'subject: \n ': 0.16; 'variable.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; "python's": 0.19; 'fit': 0.20; '(in': 0.22; 'saying': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply- To:1': 0.27; "doesn't": 0.30; 'matching': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'implicit': 0.31; '(including': 0.33; 'implemented': 0.33; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'words,': 0.36; 'yield': 0.36; 'should': 0.36; 'two': 0.37; 'being': 0.38; 'pm,': 0.38; 'does': 0.39; 'how': 0.40; 'new': 0.61; "you're": 0.61; 'more': 0.64; 'different': 0.65; 'mar': 0.68; 'subject:this': 0.83; "'for'": 0.84; 'closes': 0.84; 'concept.': 0.84; 'effectively,': 0.84; 'carries': 0.91; 'to:none': 0.92 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=mb2b06nMatNHd4csBI8Vle4EqyeRTlMOJyIlP0E9OGM=; b=pXFatLC5+/ltz0jjjlyqxqktQU6OyTaiQZm47Cb7PN1pTLbJzH4rzz/E0PWNiU1xw8 dbdMY+VHt+D85s1mGgxhMtTxvTq/gpcQhRHz2aQjoTMc4KAKrL7om8Ak77FLBTV64uzc sej4oGMYYbVqnQ8i6Dn+XXBoReDQT4nFZyA+9o+Fqpj6LUGu7pi5IUhPgVDE+jCbWx+8 JHVQirTLaU5S/UdIMpj4xnudmM9LZxaUY6xWQwDzrf9cLOuVa5XwiXketLU5XhVY2Ud+ Km8YpqCPsz6Px1TeyuR6HSX6JcVjFQB2f+/NsDukPg+N+AkrJBnsUZRAR1ECiYGXph8Z FQ2w== MIME-Version: 1.0 X-Received: by 10.66.164.229 with SMTP id yt5mr58920435pab.67.1395463873678; Fri, 21 Mar 2014 21:51:13 -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 15:51:13 +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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395463877 news.xs4all.nl 2834 [2001:888:2000:d::a6]:38013 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68745 On Sat, Mar 22, 2014 at 3:39 PM, Rustom Mody wrote: >> 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) > > Thats using a for-loop > A 'for' in a comprehension carries a different intention, the matching names > being merely coincidental. So what you're saying is that these two are fundamentally different: def unrolled(): x = 1 yield (lambda: x) x = 2 yield (lambda: x) x = 3 yield (lambda: x) def loop(): for x in 1,2,3: yield (lambda: x) In other words, a loop should be implemented as a separate binding each time, not a rebinding. That's an interesting point, and it does make some sense; effectively, what you want is for the body of a for loop to be a new scope, a unique scope every iteration of the loop, and one that automatically closes over all variables in the parent scope (including for assignments) except for the loop iteration/counter variable. That does make some sense, but it doesn't really fit Python's concept. It would, however, fit a more C-like language, where locals are declared (in Python, you'd have to put a whole lot of implicit 'nonlocal' statements at the top of the loop). ChrisAg