Sure, you can technically do that, but you'll have to rewrite most of the symtable pass in the compiler, since you won't be able to re-use the current recursive descent algorithms (which are designed to handle function scopes). That ran afoul of "If the implementation is hard to explain, it's a bad idea."
Where that gets particularly nasty is when you nest comprehensions with the *same* iteration variable name:
>>> [[i for i in range(2)] for i in range(2)]
[[0, 1], [0, 1]]
Now your name generator needs to be clever enough to account for how many levels deep it is. You also run into the lexical scoping problem anyway once a lambda expression shows up:
>>> [[(lambda i=i: i)() for i in range(2)] for i in range(2)]
[[0, 1], [0, 1]]
>>> [[(lambda: i)() for i in range(2)] for i in range(2)]
[[0, 1], [0, 1]]
As noted earlier, not impossible to resolve, but for from the "simple" option that folks seem to believe it is (and which I thought it would be before I actually tried to implement it while respecting Python's lexical scoping rules). |