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


Groups > comp.lang.python > #12328

Why do closures do this?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <johnmohagan@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.045
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; 'thread,': 0.04; '"default': 0.09; 'function:': 0.09; 'examples': 0.11; 'def': 0.15; '"f"': 0.16; '"n"': 0.16; 'closures,': 0.16; 'expression,': 0.16; 'this:': 0.16; 'seems': 0.20; 'tells': 0.21; 'refers': 0.23; 'explains': 0.24; 'sender:addr:gmail.com': 0.25; 'preferred': 0.25; "i'm": 0.27; 'function': 0.27; 'bound': 0.29; 'definition': 0.30; 'subject:?': 0.31; 'least': 0.31; 'values': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; "i've": 0.34; 'surprised': 0.34; 'google': 0.36; 'charset:us-ascii': 0.36; 'question': 0.36; 'using': 0.37; 'but': 0.37; 'two': 0.37; 'somewhat': 0.38; 'some': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'ways': 0.39; 'header:Mime-Version:1': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; "it's": 0.40; 'where': 0.40; 'header :Message-Id:1': 0.61; 'john': 0.62; 'factory': 0.73; 'subject:this': 0.74; 'right),': 0.84; 'surprised,': 0.84; 'time"': 0.84; 'received:home': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:date:from:to:subject:message-id:x-mailer:mime-version :content-type:content-transfer-encoding; bh=7ZzMqgoVMi+pMTBLh/yAcp0WkBOQ3j0+VsvyxOHVVhg=; b=cOf1eafbCZPBtLhOBJ0l9yqJaTtoV4vlEQdIaHfSwDAItY3djOrKlnFdv/Lke34qid NbWIhlog1eLy0NkZiJCW9Wo1eQoTqu5OGyKSPPnEpemD7yuIFGJnOCqyUQ6/v3skywbZ oiGTTAf08s/Cdcd4Hu+onTAvY87EN2aOFWsV4=
Sender "John O'Hagan" <johnmohagan@gmail.com>
Date Sun, 28 Aug 2011 13:45:05 +1000
From John O'Hagan <research@johnohagan.com>
To python-list@python.org
Subject Why do closures do this?
X-Mailer Sylpheed 3.2.0beta2 (GTK+ 2.24.4; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
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>
Newsgroups comp.lang.python
Message-ID <mailman.500.1314503122.27778.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314503122 news.xs4all.nl 2438 [2001:888:2000:d::a6]:55968
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12328

Show key headers only | View raw


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: either make a factory function:

def mkfnc(n):
    def fnc():
        return n
    return fnc

funcs=[]
for n in range(3):
    funcs.append(mkfnc(n))

which seems roundabout, or take advantage of the "default values set at definition time" behaviour:

funcs=[]
for n in range(3):
    def f(n=n):
        return n
    funcs.append(f)

which seems obscure, and a side-effect.

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?

Regards,

John 

Back to comp.lang.python | Previous | NextNext 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