Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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; 'argument': 0.05; 'subject:Python': 0.06; '21,': 0.07; 'python3': 0.07; 'ugly': 0.07; 'variables': 0.07; 'subject:language': 0.09; 'wrapper': 0.09; 'python': 0.11; 'def': 0.12; 'binding.': 0.16; 'lambda': 0.16; 'subject: \n ': 0.16; 'weird': 0.16; 'language': 0.16; 'wrote:': 0.18; 'variable': 0.18; '>>>': 0.22; 'header:In-Reply- To:1': 0.27; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'layer': 0.31; 'worked': 0.33; 'fri,': 0.33; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'too': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'name:': 0.61; 'mar': 0.68; 'default': 0.69; 'behavior': 0.77; 'subject:this': 0.83; 'confusing': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=0fZHZCRfMQyq3NEDz49N+Gn/k72DC/D/51LlqVnHD7w=; b=gYeZNgea6/wx1NpwpquDPa0SWvGM+ic4lt9guKeKaYfuGK7hpJxRZSYcXOqmeIc6OE 4PmGbpYRNKXxawXmEaPmDSHwU/FfmAcEJ5AnxAWFIw81ofB4Pe2enTkhy/PVaI9dQqvg 5DGWuVrsbjG4U2S0JpWqf9iAPo5e0HnUH/IBa0nLf035nnVMkDmLLfMxAP27mANeqvRd gYGRE1ctUHBxGkh/AjuPRanJGOyCeHmoy2VSlX9no5fqFwLYolyrciWPDvjjSntrTDCQ VspYZE4MoaEWQXyMrvsvCYFBoLb8MH4Z5K/PAPd/t+kLRgurR1ALEOgD6FYokTBT59Bu qunA== X-Received: by 10.68.137.136 with SMTP id qi8mr58675908pbb.79.1395479437073; Sat, 22 Mar 2014 02:10:37 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> From: Ian Kelly Date: Sat, 22 Mar 2014 03:09:56 -0600 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395479446 news.xs4all.nl 2913 [2001:888:2000:d::a6]:52025 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68756 On Fri, Mar 21, 2014 at 8: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 Because Python as a language only has the concept of assignment, not binding. I think it would be weird and confusing if variables worked this way in comprehensions and nowhere else. > >>> fl = [lambda y : x+y for x in [1,2,3]] > >>> [fl[i](2) for i in [0,1,2]] > [5, 5, 5] You can get the desired effect by adding a layer of indirection: >>> fl = [(lambda x: lambda y: x+y)(x) for x in [1,2,3]] >>> [f(2) for f in fl] [3, 4, 5] If that's too ugly then give the wrapper a proper name: >>> def make_function(x): ... return lambda y: x+y ... >>> fl = [make_function(x) for x in [1,2,3]] >>> [f(2) for f in fl] [3, 4, 5] There is also the default argument trick: >>> fl = [lambda y, *, x=x: x+y for x in [1,2,3]] >>> [f(2) for f in fl] [3, 4, 5]