Guido van Rossum email@example.com writes:
On Monday 20 October 2003 04:30 pm, Guido van Rossum wrote:
We are indeed sure (sadly) that list comprehensions leak control variable names.
But they shouldn't. It can be fixed by renaming them (e.g. numeric names with a leading dot).
[.2 for .2 in range(3)]
SyntaxError: can't assign to literal
I think I don't understand what you mean.
I meant that the compiler should rename it.
Implementing this might be entertaining. In particular what happens if the iteration variable is a local in the frame anyway? I presume that would inhibit the renaming, but then there's a potentially confusing dichotomy as to whether renaming gets done. Of course you could *always* rename, but then code like
def f(x): r = [x+1 for x in range(x)] return r, x
becomes even more incomprehensible (and changes in behaviour).
And what about horrors like
[([x for x in range(10)],x) for x in range(10)]
[([x for x in range(10)],y) for y in range(10)]
I suppose you could make a case for throwing out (or warning about) all these cases at compile time, but that would require significant effort as well (I think).