Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'preferred.': 0.04; 'thread,': 0.04; 'behave': 0.07; 'function,': 0.07; 'message- id:@glegroupsg2000goo.googlegroups.com': 0.09; 'reply- to:addr:comp.lang.python': 0.09; 'to:addr:comp.lang.python': 0.09; 'examples': 0.11; 'def': 0.15; 'case.': 0.15; '"f"': 0.16; '"n"': 0.16; 'closures,': 0.16; 'expression,': 0.16; 'cc:addr:python- list': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.20; 'tells': 0.21; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; '0.1': 0.23; 'refers': 0.23; 'explains': 0.24; 'creating': 0.25; 'preferred': 0.25; "i'm": 0.27; 'function': 0.27; 'seeing': 0.28; 'loop': 0.28; '27,': 0.29; 'bound': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; 'definition': 0.30; 'carl': 0.30; 'subject:?': 0.31; 'least': 0.31; 'there': 0.33; "i've": 0.34; 'header:User-Agent:1': 0.34; 'surprised': 0.34; 'google': 0.36; 'question': 0.36; 'using': 0.37; 'but': 0.37; 'two': 0.37; '(not': 0.38; 'strong': 0.38; 'somewhat': 0.38; 'some': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'ways': 0.39; 'case': 0.39; "it's": 0.40; 'where': 0.40; 'more': 0.60; 'john': 0.62; 'august': 0.70; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; 'expectations': 0.73; 'reply-to:addr:googlegroups.com': 0.74; 'subject:this': 0.74; 'right),': 0.84; 'surprised,': 0.84 Newsgroups: comp.lang.python Date: Sun, 28 Aug 2011 14:16:30 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=76.91.176.167; posting-account=XFEWnwoAAADNO10m3Wcmq_SWdmyZuXff References: User-Agent: G2/1.0 X-Google-Web-Client: true MIME-Version: 1.0 Subject: Re: Why do closures do this? From: Carl Banks To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: comp.lang.python@googlegroups.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314566198 news.xs4all.nl 2521 [2001:888:2000:d::a6]:47163 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12350 On Saturday, August 27, 2011 8:45:05 PM UTC-7, John O'Hagan wrote: > Somewhat apropos of the recent "function principle" thread, I was recentl= y surprised by this: >=20 > funcs=3D[] > for n in range(3): > def f(): > return n > funcs.append(f) >=20 > [i() for i in funcs] >=20 > The last expression, IMO surprisingly, is [2,2,2], not [0,1,2]. Google te= lls me I'm not the only one surprised, but explains that it's because "n" i= n the function "f" refers to whatever "n" is currently bound to, not what i= t was bound to at definition time (if I've got that right), and that there = are at least two ways around it: .... > My question is, is this an inescapable consequence of using closures, or = is it by design, and if so, what are some examples of where this would be t= he preferred behaviour? It is the preferred behavior for the following case. def foo(): def printlocals(): print a,b,c,d a =3D 1; b =3D 4; c =3D 5; d =3D 0.1 printlocals() a =3D 2 printlocals() When seeing a nested function, there are strong expectations by most people= that it will behave this way (not to mention it's a lot more useful). It'= s only for the less common and much more advanced case of creating a closur= e in a loop that the other behavior would be preferred. Carl Banks