Generator Comprehensions

hamish_lawson hamish_lawson at yahoo.co.uk
Tue Jan 29 10:23:39 EST 2002


Skip Montanaro wrote:

> > It has always bothered me that the temporary variables inside 
> > list comprehensions are still visible afterwards (and overwrite 
> > existing variables):
> 
> I don't see any spurious locals:
> 
>     >>> def f(n):
>     ...   l = [i for i in range(n)]
>     ...   print locals()
>     ...   return l
>     ... 
>     >>> f(5)
>     {'i': 4, 'l': [0, 1, 2, 3, 4], 'n': 5}
>     [0, 1, 2, 3, 4]

I think the original author was referring to the very fact that 'i' 
is among the locals that you printed out (he presumably would rather 
that a list comprehension was a self-contained scope).

Hamish Lawson






More information about the Python-list mailing list