Generator Comprehensions
Just van Rossum
just at xs4all.nl
Tue Jan 29 10:21:41 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):
>
> Jason> Wow. I wasn't aware of that. But now that I am, it bothers me.
>
> 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]
>
> When you disassemble the function you see a strange local named "_[1]":
> [ ...schnipp... ]
It's not about "_[1]", but about "i": it's not completely obvious (to me at
least) that names used in the comprehension should be part of the local scope.
It doesn't bother me a *lot* that they are, but at the same time I don't see
any use for it either.
Just
More information about the Python-list
mailing list