Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.08; '>>>>': 0.09; 'iterate': 0.09; 'pm,': 0.10; '>>>': 0.12; 'wrote:': 0.14; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '[gcc': 0.16; 'linux2': 0.16; 'scope,': 0.16; 'to:addr:gmx.net': 0.16; 'cc:addr:python-list': 0.17; 'tue,': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'variable': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'received:209.85.161.46': 0.23; 'received:mail-fx0-f46.google.com': 0.23; 'values': 0.25; 'received:209.85.161': 0.26; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.30; 'apr': 0.32; 'martin': 0.32; 'list': 0.33; 'takes': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'subject:: ': 0.38; 'received:209': 0.39; 'more': 0.60; '31,': 0.65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=JLZnxpkq+ZGGPl1Osd2cU7x4LAl1gNe7oDb91ZEOGP4=; b=gjygAIh8RLNw/3i1CYMCAKUugXqu06IfDUER0Q11bEmvLl0zKhK8wfkN4cZEK+bGm2 tR16VfVoNnvx7eyje9WkyqEZ7xvdPa9cDLYgLAvR8mRvNiQSlyQP29X5GRfIcXdKwacK zLCKTboWUQkd+QtAavgfmpffnqXd1DoLxEwqE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=XfAUiNord6enE684ysfBvwB+2NeItj3uLIVrdcBSKzVkNCQ87J7Zv3IvIT1uhoPw/i aFD7S4jX82Vz7NBU29ljnasxDADG6o2ZxukovSz9weUwzMS/5NM6Oj4gMOID11d+LIhp u6w3kXMqTbsQS8uTcEEw8Eun1WxIZDNml4pa0= MIME-Version: 1.0 In-Reply-To: <20110531231456.44d388d7@Fuddel> References: <20110531231456.44d388d7@Fuddel> From: Ian Kelly Date: Tue, 31 May 2011 15:47:33 -0600 Subject: Re: Something is rotten in Denmark... To: Martin Manns Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 26 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306878486 news.xs4all.nl 49181 [::ffff:82.94.164.166]:37254 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6757 On Tue, May 31, 2011 at 3:14 PM, Martin Manns wrote: > $ python > Python 2.6.6 (r266:84292, Apr 20 2011, 11:58:30) > [GCC 4.5.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> fs=[] >>>> fs = [(lambda n: i + n) for i in range(10)] >>>> [fs[i](1) for i in range(10)] > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] This works by accident. >>> [fs[i](1) for i in range(10)] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> [fs[0](1) for i in range(10)] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> [f(1) for f in fs] [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] The i variable is part of the global scope, and as you iterate over range(10) again it coincidentally takes on the same values as in the original list comprehension. You don't see this in Python 3 because the scope of i is limited to the list comprehension, not global. Cheers, Ian