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


Groups > comp.lang.python > #12344

Re: Why do closures do this?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'function,': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'ugly': 0.07; 'python': 0.08; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'situation,': 0.09; 'am,': 0.12; 'def': 0.15; 'class,': 0.15; 'cryptic': 0.16; 'did,': 0.16; 'f()': 0.16; 'infinitely': 0.16; 'reedy': 0.16; 'surrogate': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'defined': 0.19; 'jan': 0.19; 'assume': 0.22; 'header:In-Reply-To:1': 0.22; 'changed': 0.24; 'variable': 0.24; 'code': 0.25; 'function': 0.27; 'definition': 0.30; 'do.': 0.30; 'differently': 0.30; 'parent': 0.30; '\xe2\x80\x94': 0.30; 'moving': 0.31; 'subject:?': 0.31; 'changes': 0.31; 'least': 0.31; 'values': 0.32; 'necessary.': 0.32; 'does': 0.32; 'to:addr:python-list': 0.33; 'that,': 0.33; 'however,': 0.34; 'header:User-Agent:1': 0.34; 'function.': 0.34; 'header:X-Complaints-To:1': 0.35; 'certain': 0.35; 'hack': 0.37; 'variables': 0.37; 'using': 0.37; 'list,': 0.37; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'or,': 0.40; 'extremely': 0.40; 'more': 0.60; 'view': 0.67; 'situations.': 0.73; 'subject:this': 0.74; 'acts': 0.77; '10:04': 0.84; 'common,': 0.84; 'dict,': 0.84; 'radically': 0.84; 'absolutely': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Why do closures do this?
Date Sun, 28 Aug 2011 12:26:18 -0400
References <20110828134505.795cd32b8fc5ccc472f85ae3@johnohagan.com> <4E5A4B08.2050405@jollybox.de>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0
In-Reply-To <4E5A4B08.2050405@jollybox.de>
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.507.1314548832.27778.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1314548832 news.xs4all.nl 2548 [2001:888:2000:d::a6]:38073
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12344

Show key headers only | View raw


On 8/28/2011 10:04 AM, Thomas Jollans wrote:

> This does not do what you'd like it to do. But let's assume that, it
> did, that Python, when encountering a function definition inside a
> function, "froze" the values of nonlocal variables used in the new
> function, from the point of view of that function — that *might* be more
> intuitive, at least in certain situations. However, what if you wanted a
> closure to access a nonlocal variable that changes - acting differently
> depending on what has since happened in the parent function.
>
> That may not be as common, but it is a perfectly plausible situation,
> and the hack required to support that behaviour in a Python that acts as
> you had expected it to, a surrogate namespace using a class, list, or
> dict, is infinitely more cryptic and ugly than the default-parametre hack.

Or, what if the nonlocal name is not even defined when the closure is.

 >>> def f():
	def g(): print(a)
	a = 3
	g()

 >>> f()
3

Note that global names also do not have to be defined when a function 
using them is compiled. The current situation is that nonlocal and 
global names are treated the same way. This makes normal and nested 
functions as much the same as possible. This is intentional. It would be 
extremely disconcerting if moving code that contains a def into or out 
of a wrapping function radically changed its behavior more than is 
absolutely necessary.

-- 
Terry Jan Reedy

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


Thread

Re: Why do closures do this? Terry Reedy <tjreedy@udel.edu> - 2011-08-28 12:26 -0400

csiph-web