Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.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.036 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'subject:Python': 0.06; 'fix.': 0.09; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; '(lambda': 0.16; 'closure,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; '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; 'does': 0.39; 'even': 0.60; 'easy': 0.60; 'lower': 0.61; 'first': 0.61; 'here': 0.66; 'mar': 0.68; 'difficulty': 0.68; 'subject:this': 0.83; '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=QVBcFNHJnxj/inSuR6lBT098AlfXLNRaR8B42HMA4EY=; b=ClhJpaWdOMWHfiL7huvnCc8MpSw3q8hcdczSRbyjTKL68c+HFn7J8xO9taS1xWFWfK 9BnqX4+nb0Xvn6WfCfcoFSH5f8/5pDg0UTz5nnN74sjfCwv0pdNre0ugsN7AhpliZTMv nwD72PfEcZlqV1cGZzXCtl7HoW2+16tcGigvcmnTClKOdy0IBeayugkiYBalPtjSZiKK YBeY17rOJzMhgwlX3Af2Lys0oTabzfkfvVMfyd04K5fsLbycJnhEPGWBq/5XzMFNOW7H uGpiqUYM9JGUMgZNUpZSkb6FFjBxYYLv7sq4Ib/0AS3C0qhoxZX2I8cETOiSLHqpap8j 44xQ== MIME-Version: 1.0 X-Received: by 10.68.112.164 with SMTP id ir4mr6531193pbb.153.1395702088137; Mon, 24 Mar 2014 16:01:28 -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:01: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: 14 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395702091 news.xs4all.nl 2937 [2001:888:2000:d::a6]:53757 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68910 On Tue, Mar 25, 2014 at 8:43 AM, Mark H Harris wrote: >> adders[n] = (lambda b: lambda a: b + a)(n) > > Now, here is where I talk about confusion; explaining why the first > lambda above does not work because of scope and closure, and then even > worse, explaining why the second "double" lambda works in the lower example! 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! ChrisA