Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12350

Re: Why do closures do this?

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 <pavlovevidence@gmail.com>
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 <mailman.500.1314503122.27778.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=76.91.176.167; posting-account=XFEWnwoAAADNO10m3Wcmq_SWdmyZuXff
References <mailman.500.1314503122.27778.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
MIME-Version 1.0
Subject Re: Why do closures do this?
From Carl Banks <pavlovevidence@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.515.1314566198.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Saturday, August 27, 2011 8:45:05 PM UTC-7, John O&#39;Hagan wrote:
> Somewhat apropos of the recent "function principle" thread, I was recently surprised by this:
> 
> funcs=[]
> for n in range(3):
>     def f():
>         return n
>     funcs.append(f)
> 
> [i() for i in funcs]
> 
> The last expression, IMO surprisingly, is [2,2,2], not [0,1,2]. Google tells me I'm not the only one surprised, but explains that it's because "n" in the function "f" refers to whatever "n" is currently bound to, not what it 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 the preferred behaviour?


It is the preferred behavior for the following case.

def foo():
    def printlocals():
        print a,b,c,d
    a = 1; b = 4; c = 5; d = 0.1
    printlocals()
    a = 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 closure in a loop that the other behavior would be preferred.


Carl Banks

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why do closures do this? John O'Hagan <research@johnohagan.com> - 2011-08-28 13:45 +1000
  Re: Why do closures do this? Carl Banks <pavlovevidence@gmail.com> - 2011-08-28 14:16 -0700
  Re: Why do closures do this? Carl Banks <pavlovevidence@gmail.com> - 2011-08-28 14:16 -0700

csiph-web