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.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:Python': 0.06; 'fix.': 0.09; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '(1,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'it;': 0.16; 'lambda': 0.16; 'subject: \n ': 0.16; 'variable.': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'value.': 0.19; 'work,': 0.20; '>>>': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'dec': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; '25,': 0.31; 'received:google.com': 0.35; 'there': 0.35; 'are,': 0.36; 'words,': 0.36; 'pm,': 0.38; 'that,': 0.38; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'easy': 0.60; 'experts': 0.60; 'first': 0.61; 'mar': 0.68; 'difficulty': 0.68; '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=/K7SXkb79RC/eWhXyFAG4hK6ktxJZx+OE93o1mITnoY=; b=tYowkHSExlEK6/TBzIemgWUo40T3zkXuzr5Aoz/cOrf738ZqnrbBKfEHpYItkUXF+h Zb9AIHPX/yIuXE06LSx5tE2cOdUa17LCufjViibKs0rlf3S/XRWCaF1ATgmsna4br3Mw iPnXbuLxCuQUzD7oQThnbmYmEUWk9PVDwxNkhtspCl1L6mv4o5lWtV2DvpLV81XljfC4 H3hGwgIFiFhy3sCUWAMdXB1DZ9fCKqvA2zl+hehnyGkjuqRow4xwBpy+HlXet/HMXjqu PR2mz2RGNJnrO5Z6DXJ7lrCxmqaVgSjdz/ES24LH53JVeSUKr9bs2J5lC0UizYaopHlD pKSA== MIME-Version: 1.0 X-Received: by 10.66.129.133 with SMTP id nw5mr73926555pab.98.1395705454654; Mon, 24 Mar 2014 16:57:34 -0700 (PDT) In-Reply-To: References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <532d5bd9$0$29994$c3e8da3$5496439d@news.astraweb.com> Date: Tue, 25 Mar 2014 10:57:34 +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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395705463 news.xs4all.nl 2851 [2001:888:2000:d::a6]:47055 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68916 On Tue, Mar 25, 2014 at 10:44 AM, Mark H Harris wrote: > On 3/24/14 6:01 PM, Chris Angelico wrote: > >> Easy fix. Use the "explicit capture" notation: >> adders[n] = lambda a, n=n: a+n >> And there you are, out of your difficulty at once! > But, and this is the big (WHY?) is that necessary? In other words, its > not about experts knowing how to make this work, its about "normal" people > not understanding in the first place why its a problem, and why the various > solutions work to fix it; even though "we" all know that nothing is > 'broken'. Why is it necessary? For the same reason that this works: 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) When you use the variable x in multiple places, it's the same variable and it has a single value. If you don't want that, you have to make a separate variable. ChrisA