Odd name shadowing in comprehension

Chris Angelico rosuav at gmail.com
Sun Oct 23 01:47:44 EDT 2016


On Sun, Oct 23, 2016 at 4:08 PM, eryk sun <eryksun at gmail.com> 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. Comprehensions have adopted the same
> behavior, and it's not a bug or a flawed design. However it does need
> to be documented better in section 6.2.4.

It may not be flawed design, but it's certainly unintuitive, given the
normal explanation:

gen = ((x,y) for x in range(y) for y in range(z))

def g():
    for x in range(y):
        for y in range(z):
            yield (x,y)
gen = g()

It's actually this, which is definitely unintuitive:

def g(it):
    for x in it:
        for y in range(z):
            yield (x,y)
gen = g(iter(range(y)))

Do you ever see them explained like that? Me neither. So if it's
intentional, it needs to be explained somewhere, unless the assumption
is that it'll almost never matter.

ChrisA



More information about the Python-list mailing list