Odd name shadowing in comprehension
Chris Angelico
rosuav at gmail.com
Sat Oct 22 23:26:58 EDT 2016
On Sun, Oct 23, 2016 at 2:14 PM, Steve D'Aprano
<steve+python at pearwood.info> 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 strangely there's no error:
>
Right. Disassembly shows that it's something like this:
y = 6
val1 = [(x,y) for x in range(y) for y in range(3)]
def temp(iter):
ret = []
for x in iter:
for y in range(3):
ret.append((x, y))
return ret
val2 = temp(iter(range(y)))
That first iterable is evaluated in the outer context, then passed as
a parameter.
ChrisA
More information about the Python-list
mailing list