Re: Incorrect scope of list comprehension variables

2010-04-19 Thread Duncan Booth
Dave Angel wrote: > 2) In original C, and I think in C++, the lifetime of i lasted long > after the loop ended. > for (int i=0; i< limit; ++i) > { >z += i; > } > i is still valid after this curly brace > > In C99, and at least in later C++, the scope of i ends w

Re: Incorrect scope of list comprehension variables

2010-04-17 Thread Steven D'Aprano
On Sat, 17 Apr 2010 17:23:45 +0200, Alain Ketterlin wrote: > Steven D'Aprano writes: > >> On Sat, 17 Apr 2010 12:05:03 +0200, Alain Ketterlin wrote: >> I don't know of any language that creates a new scope for loop variables, but perhaps that's just my ignorance... >>> >>> I think Pas

Re: Incorrect scope of list comprehension variables

2010-04-17 Thread Ethan Furman
There's no RightAnswer(tm), just our best guess as to what is the most useful behavior for the most number of people. +1 QOTW -- http://mail.python.org/mailman/listinfo/python-list

Re: Incorrect scope of list comprehension variables

2010-04-17 Thread Alain Ketterlin
Steven D'Aprano writes: > On Sat, 17 Apr 2010 12:05:03 +0200, Alain Ketterlin wrote: > >>> I don't know of any language that creates a new scope for loop >>> variables, but perhaps that's just my ignorance... >> >> I think Pascal and Modula-2 do this, Fortran does this, as well as Ada. > > Pasca

Re: Incorrect scope of list comprehension variables

2010-04-17 Thread Steven D'Aprano
On Sat, 17 Apr 2010 12:05:03 +0200, Alain Ketterlin wrote: >> I don't know of any language that creates a new scope for loop >> variables, but perhaps that's just my ignorance... > > I think Pascal and Modula-2 do this, Fortran does this, as well as Ada. Pascal doesn't do this. [st...@sylar pas

Re: Incorrect scope of list comprehension variables

2010-04-17 Thread Alain Ketterlin
Steven D'Aprano writes: > On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote: > >>>Nevertheless, it is a common intuition that the list comp variable >>>should *not* be exposed outside of the list comp, and that the for-loop >>>variable should. Perhaps it makes no sense, but it is very common -- >>>I

Re: Incorrect scope of list comprehension variables

2010-04-17 Thread Dave Angel
Steven D'Aprano wrote: On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote: In article <4bb92850$0$8827$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: Nevertheless, it is a common intuition that the list comp variable should *not* be exposed outside of the list comp, and that the for-l

Re: Incorrect scope of list comprehension variables

2010-04-16 Thread Alf P. Steinbach
* Steven D'Aprano: On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote: In article <4bb92850$0$8827$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: Nevertheless, it is a common intuition that the list comp variable should *not* be exposed outside of the list comp, and that the for-loop variable

Re: Incorrect scope of list comprehension variables

2010-04-16 Thread MRAB
Steven D'Aprano wrote: On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote: In article <4bb92850$0$8827$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: Nevertheless, it is a common intuition that the list comp variable should *not* be exposed outside of the list comp, and that the for-loop vari

Re: Incorrect scope of list comprehension variables

2010-04-16 Thread Chris Rebert
On Fri, Apr 16, 2010 at 5:20 PM, Steven D'Aprano wrote: > On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote: >> In article <4bb92850$0$8827$c3e8...@news.astraweb.com>, Steven D'Aprano >> wrote: >>>Nevertheless, it is a common intuition that the list comp variable >>>should *not* be exposed outside o

Re: Incorrect scope of list comprehension variables

2010-04-16 Thread Steven D'Aprano
On Fri, 16 Apr 2010 08:48:03 -0700, Aahz wrote: > In article <4bb92850$0$8827$c3e8...@news.astraweb.com>, Steven D'Aprano > wrote: >> >>Nevertheless, it is a common intuition that the list comp variable >>should *not* be exposed outside of the list comp, and that the for-loop >>variable should.

Re: Incorrect scope of list comprehension variables

2010-04-16 Thread Raymond Hettinger
On Apr 3, 3:30 am, Alain Ketterlin wrote: > Hi all, > > I've just spent a few hours debugging code similar to this: > > d = dict() > for r in [1,2,3]: >     d[r] = [r for r in [4,5,6]] > print d > > THe problem is that the "r" in d[r] somehow captures the value of the > "r" in the list comprehensi

Re: Incorrect scope of list comprehension variables

2010-04-16 Thread Aahz
In article <4bb92850$0$8827$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: > >Nevertheless, it is a common intuition that the list comp variable should >*not* be exposed outside of the list comp, and that the for-loop variable >should. Perhaps it makes no sense, but it is very common -- I'v

Re: Incorrect scope of list comprehension variables

2010-04-06 Thread Rolando Espinoza La Fuente
On Sun, Apr 4, 2010 at 5:20 PM, Paul Rubin wrote: [...] > >    d[r] = list(r for r in [4,5,6]) > This have a slightly performance difference. I think mainly the generator's next() call. In [1]: %timeit list(r for r in range(1)) 100 loops, best of 3: 2.78 ms per loop In [2]: %timeit [r for r

Re: Incorrect scope of list comprehension variables

2010-04-06 Thread Gregory Ewing
Lie Ryan wrote: in python there is only a flat local namespace and the names resolver becomes a thousand times simpler No, it doesn't. The compiler already has to deal with multiple scopes for nested functions. There may be some simplification, but not a lot. The main reason is linguistic. Hav

Re: Incorrect scope of list comprehension variables

2010-04-06 Thread Stephen Hansen
On 2010-04-06 09:34:04 -0700, Lie Ryan said: in python there is only a flat local namespace and the names resolver becomes a thousand times simpler (and faster). This hasn't been true for a long time. Yes, local variables are optimized to be indexed in an array, but Python has nested scopes f

Re: Incorrect scope of list comprehension variables

2010-04-06 Thread Lie Ryan
On 04/06/10 18:42, Alain Ketterlin wrote: > Alain Ketterlin writes: > >> d = dict() >> for r in [1,2,3]: >> d[r] = [r for r in [4,5,6]] >> print d > > Thanks to Chris and Paul for the details (the list comp. r actually > leaks). I should have found this by myself. > > My background is more

Re: Incorrect scope of list comprehension variables

2010-04-06 Thread Alain Ketterlin
Steven D'Aprano writes: >> d = dict() >> for r in [1,2,3]: >> d[r] = [r for r in [4,5,6]] >> print d > > This isn't directly relevant to your problem, but why use a list > comprehension in the first place? [r for r in [4,5,6]] is just [4,5,6], > only slower. Sure. But I've actually spent s

Re: Incorrect scope of list comprehension variables

2010-04-06 Thread Alain Ketterlin
Alain Ketterlin writes: > d = dict() > for r in [1,2,3]: > d[r] = [r for r in [4,5,6]] > print d Thanks to Chris and Paul for the details (the list comp. r actually leaks). I should have found this by myself. My background is more on functional programming languages, that's why I thought th

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Stephen Hansen
On 2010-04-04 14:50:54 -0700, Paul Rubin said: Alain Ketterlin writes: d[r] = [r for r in [4,5,6]] THe problem is that the "r" in d[r] somehow captures the value of the "r" in the list comprehension, and somehow kills the loop interator. Yes, this is a well known design error in Python 2.x.

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Stephen Hansen
On 2010-04-04 17:01:20 -0700, Steven D'Aprano said: On Sun, 04 Apr 2010 05:50:01 -0700, Ethan Furman wrote: Yes, this has been fixed in later revisions, but I'm curious to know what led you to believe that a list comprehension created a new scope. I don't that was ever promised. Common sens

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Steven D'Aprano
On Sun, 04 Apr 2010 14:50:54 -0700, Paul Rubin wrote: > Alain Ketterlin writes: >> d[r] = [r for r in [4,5,6]] >> THe problem is that the "r" in d[r] somehow captures the value of the >> "r" in the list comprehension, and somehow kills the loop interator. > > Yes, this is a well known design

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Steven D'Aprano
On Sun, 04 Apr 2010 05:50:01 -0700, Ethan Furman wrote: >>>Yes, this has been fixed in later revisions, but I'm curious to know >>>what led you to believe that a list comprehension created a new scope. >>>I don't that was ever promised. >> >> >> Common sense about how programming languages shou

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Paul Rubin
Alain Ketterlin writes: > d[r] = [r for r in [4,5,6]] > THe problem is that the "r" in d[r] somehow captures the value of the > "r" in the list comprehension, and somehow kills the loop interator. Yes, this is a well known design error in Python 2.x. The 3.x series fixes this error but intro

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Brian Blais
On Apr 4, 2010, at 3:17 , Stephen Hansen wrote: Where exactly does this common sense come from? A list comprehension is basically syntactic sugar over a for loop, and... well, since I've been bitten by this particular wart, I was surprised to see that the list comp didn't have it's own sco

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread D'Arcy J.M. Cain
On Sun, 04 Apr 2010 15:06:46 +0200 "Alf P. Steinbach" wrote: > Common sense is applied first, as a heuristic. You really wouldn't want to > drill > down into the architect's drawings in order to get office 215 in a building. > First you apply common sense. Oh goodie, bad analogies. Can I play

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Alf P. Steinbach
* Ethan Furman: Steve Howell wrote: On Apr 3, 9:58 pm, Tim Roberts wrote: Alain Ketterlin wrote: I've just spent a few hours debugging code similar to this: d = dict() for r in [1,2,3]: d[r] = [r for r in [4,5,6]] print d Yes, this has been fixed in later revisions, but I'm curious

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Ethan Furman
Steve Howell wrote: On Apr 3, 9:58 pm, Tim Roberts wrote: Alain Ketterlin wrote: I've just spent a few hours debugging code similar to this: d = dict() for r in [1,2,3]: d[r] = [r for r in [4,5,6]] print d Yes, this has been fixed in later revisions, but I'm curious to know what led

Re: Incorrect scope of list comprehension variables

2010-04-04 Thread Stephen Hansen
On 2010-04-03 23:30:32 -0700, Steve Howell said: On Apr 3, 9:58 pm, Tim Roberts wrote: Alain Ketterlin wrote: I've just spent a few hours debugging code similar to this: d = dict() for r in [1,2,3]:    d[r] = [r for r in [4,5,6]] print d Yes, this has been fixed in later revisions, but

Re: Incorrect scope of list comprehension variables

2010-04-03 Thread Steve Howell
On Apr 3, 9:58 pm, Tim Roberts wrote: > Alain Ketterlin wrote: > > >I've just spent a few hours debugging code similar to this: > > >d = dict() > >for r in [1,2,3]: > >    d[r] = [r for r in [4,5,6]] > >print d > > Yes, this has been fixed in later revisions, but I'm curious to know what > led yo

Re: Incorrect scope of list comprehension variables

2010-04-03 Thread Tim Roberts
Alain Ketterlin wrote: > >I've just spent a few hours debugging code similar to this: > >d = dict() >for r in [1,2,3]: >d[r] = [r for r in [4,5,6]] >print d Yes, this has been fixed in later revisions, but I'm curious to know what led you to believe that a list comprehension created a new sco

Re: Incorrect scope of list comprehension variables

2010-04-03 Thread Steven D'Aprano
On Sat, 03 Apr 2010 12:30:32 +0200, Alain Ketterlin wrote: > Hi all, > > I've just spent a few hours debugging code similar to this: > > d = dict() > for r in [1,2,3]: > d[r] = [r for r in [4,5,6]] > print d This isn't directly relevant to your problem, but why use a list comprehension in

Re: Incorrect scope of list comprehension variables

2010-04-03 Thread Chris Rebert
On Sat, Apr 3, 2010 at 3:30 AM, Alain Ketterlin wrote: > I've just spent a few hours debugging code similar to this: > > d = dict() > for r in [1,2,3]: >    d[r] = [r for r in [4,5,6]] > print d > > THe problem is that the "r" in d[r] somehow captures the value of the > "r" in the list comprehensi

Incorrect scope of list comprehension variables

2010-04-03 Thread Alain Ketterlin
Hi all, I've just spent a few hours debugging code similar to this: d = dict() for r in [1,2,3]: d[r] = [r for r in [4,5,6]] print d THe problem is that the "r" in d[r] somehow captures the value of the "r" in the list comprehension, and somehow kills the loop interator. The (unexpected) re