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


Groups > comp.lang.python > #12329

Re: Why do closures do this?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Why do closures do this?
Date 2011-08-28 00:19 -0400
References <20110828134505.795cd32b8fc5ccc472f85ae3@johnohagan.com>
Newsgroups comp.lang.python
Message-ID <mailman.501.1314505201.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 8/27/2011 11:45 PM, John O'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)
>
>
>
> 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 f(): return n
is a CONSTANT value. It is not a closure.

Your code above is the same as
def f(): return n
funcs = [f,f,f]
n = 2
[i() for i in funcs]

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

fnc is a closure and n in a nonlocal name. Since you only read it, no 
nonlocal declaration is needed.

> 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.

It was the standard idiom until nested functions were upgraded to 
enclose or capture the values of nonlocals.

> My question is, is this an inescapable consequence of using closures,

I cannot answer since I am not sure what you mean by 'this'.
Closures are nested functions that access the locals of enclosing 
functions. To ensure that the access remains possible even after the 
enclosing function returns, the last value of such accessed names is 
preserved even after the enclosing function returns. (That is the tricky 
part.)

-- 
Terry Jan Reedy

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


Thread

Re: Why do closures do this? Terry Reedy <tjreedy@udel.edu> - 2011-08-28 00:19 -0400
  Re: Why do closures do this? John Nagle <nagle@animats.com> - 2011-09-16 13:50 -0700

csiph-web