Re: Why do closures do this?

2011-09-16 Thread John Nagle
On 8/27/2011 9:19 PM, Terry Reedy wrote: 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],

Re: Why do closures do this?

2011-08-28 Thread Thomas Jollans
On 28/08/11 05:45, 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) [i() for i in funcs] The last expression, IMO surprisingly, is [2,2,2],

Re: Why do closures do this?

2011-08-28 Thread Terry Reedy
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

Re: Why do closures do this?

2011-08-28 Thread Carl Banks
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,

Why do closures do this?

2011-08-27 Thread John O'Hagan
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

Re: Why do closures do this?

2011-08-27 Thread Terry Reedy
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

Re: Why do closures do this?

2011-08-27 Thread John O'Hagan
On Sun, 28 Aug 2011 00:19:07 -0400 Terry Reedy tjre...@udel.edu wrote: 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