Re: Odd name shadowing in comprehension

2016-10-23 Thread eryk sun
On Sun, Oct 23, 2016 at 6:15 AM, Steve D'Aprano wrote: > On Sun, 23 Oct 2016 01:15 pm, eryk sun wrote: > >> I meant the behavior seems to have been copied to align with generator >> expressions, even though the cited rationale doesn't apply. I'm not >> saying this is wrong. It's useful that the ex

Re: Odd name shadowing in comprehension

2016-10-22 Thread Steve D'Aprano
On Sun, 23 Oct 2016 02:24 pm, Chris Angelico wrote: > 1) Sometimes, all the iterables can be evaluated in advance. > > dice_2d6 = [a+b for a in range(1,7) for b in range(1,7)] > > 2) But sometimes, subsequent iterables depend on the outer loop. > > triangle = [a+b for a in range(1, 7) for b in

Re: Odd name shadowing in comprehension

2016-10-22 Thread Steve D'Aprano
On Sun, 23 Oct 2016 01:15 pm, eryk sun wrote: > I meant the behavior seems to have been copied to align with generator > expressions, even though the cited rationale doesn't apply. I'm not > saying this is wrong. It's useful that the expression for the outer > iterator is evaluated in the defining

Re: Odd name shadowing in comprehension

2016-10-22 Thread Chris Angelico
On Sun, Oct 23, 2016 at 4:08 PM, eryk sun wrote: > For generator expressions, it's about early binding of the outer > iterator. This makes the result of the expression for the outer > iterator behave like a function parameter. Obviously it has to be > evaluated in the defining scope. Comprehension

Re: Odd name shadowing in comprehension

2016-10-22 Thread Terry Reedy
On 10/22/2016 11:24 PM, Chris Angelico wrote: On Sun, Oct 23, 2016 at 2:14 PM, Steve D'Aprano wrote: But it seems that the first iterator (and only that one) is evaluated in the parent context: Because the first iterator *can* always be evaluated. I don't understand what you mean by that. I

Re: Odd name shadowing in comprehension

2016-10-22 Thread eryk sun
On Sun, Oct 23, 2016 at 3:44 AM, Steve D'Aprano wrote: > > https://www.python.org/dev/peps/pep-0289/#early-binding-versus-late-binding > > But this isn't a question about early or late binding, it is asking why the > variable y is treated as both global and local in the same comprehension. > This

Re: Odd name shadowing in comprehension

2016-10-22 Thread Steve D'Aprano
On Sun, 23 Oct 2016 11:51 am, eryk sun wrote: > On Sat, Oct 22, 2016 at 11:57 PM, Chris Angelico wrote: >> >> Normally, a comprehension is described as being equivalent to an >> unrolled loop, inside a nested function. >> ... >> But it seems that the first iterator (and only that one) is evaluate

Re: Odd name shadowing in comprehension

2016-10-22 Thread Chris Angelico
On Sun, Oct 23, 2016 at 2:14 PM, Steve D'Aprano wrote: > There's definitely something strange going on. Compare the what happens when > the semi-global variable is in the first loop iterable versus the second > loop iterable. In this first example, y refers to both the global and the > local, yet

Re: Odd name shadowing in comprehension

2016-10-22 Thread Chris Angelico
On Sun, Oct 23, 2016 at 2:14 PM, Steve D'Aprano wrote: >>> But it seems that the first iterator (and only that one) is evaluated >>> in the parent context: >> >> Because the first iterator *can* always be evaluated. > > I don't understand what you mean by that. If I take you literally, it is > obv

Re: Odd name shadowing in comprehension

2016-10-22 Thread Steve D'Aprano
On Sun, 23 Oct 2016 11:43 am, Terry Reedy wrote: > On 10/22/2016 7:57 PM, Chris Angelico wrote: >> This surprised me. >> >> Python 3.4.2 (default, Oct 8 2014, 10:45:20) >> [GCC 4.9.1] on linux >> Type "help", "copyright", "credits" or "license" for more information. > y=6 > [(x,y) for x i

Re: Odd name shadowing in comprehension

2016-10-22 Thread Steve D'Aprano
On Sun, 23 Oct 2016 10:57 am, Chris Angelico wrote: > This surprised me. > > Python 3.4.2 (default, Oct 8 2014, 10:45:20) > [GCC 4.9.1] on linux > Type "help", "copyright", "credits" or "license" for more information. y=6 [(x,y) for x in range(y) for y in range(3)] > [(0, 0), (0, 1), (

Re: Odd name shadowing in comprehension

2016-10-22 Thread eryk sun
On Sun, Oct 23, 2016 at 1:28 AM, Chris Angelico wrote: > > Fair enough, except that a generator expression is syntactic sugar for > a generator function, and the return value of a generator function is > a generator object that hasn't yet been started. So where the boundary > is, then, is a bit of

Re: Odd name shadowing in comprehension

2016-10-22 Thread Chris Angelico
On Sun, Oct 23, 2016 at 11:51 AM, eryk sun wrote: > It matches the behavior of generator expressions, for which Guido > gives the following example, as quoted in PEP 289: > > Consider sum(x for x in foo()). Now suppose there's a bug in foo() > that raises an exception, and a bug in sum() t

Re: Odd name shadowing in comprehension

2016-10-22 Thread Chris Angelico
On Sun, Oct 23, 2016 at 11:43 AM, Terry Reedy wrote: >> Normally, a comprehension is described as being equivalent to an >> unrolled loop, inside a nested function. That would be like this: >> >> def temp(): >> ret = [] >> for x in range(y): >> for y in range(3): >> ret

Re: Odd name shadowing in comprehension

2016-10-22 Thread eryk sun
On Sat, Oct 22, 2016 at 11:57 PM, Chris Angelico wrote: > > Normally, a comprehension is described as being equivalent to an > unrolled loop, inside a nested function. > ... > But it seems that the first iterator (and only that one) is evaluated > in the parent context: > > Why is this? It seems r

Re: Odd name shadowing in comprehension

2016-10-22 Thread Terry Reedy
On 10/22/2016 7:57 PM, Chris Angelico wrote: This surprised me. Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. y=6 [(x,y) for x in range(y) for y in range(3)] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1,

Odd name shadowing in comprehension

2016-10-22 Thread Chris Angelico
This surprised me. Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> y=6 >>> [(x,y) for x in range(y) for y in range(3)] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), (3, 0), (3,