Generator Comprehensions

Tim Peters tim.one at home.com
Tue Jan 29 17:30:51 EST 2002


[Hamish Lawson, on the status of listcomp "index" vrbls as ordinary
 locals]
> I think the original author was referring to the very
> fact that 'i' is among the locals that you printed out.

[Skip Montanaro]
> Ah, well in that case, I can fall back on the excuse that "list
> comprehensions are essentially syntactic sugar sprinkled on for
> loops".  ;-)

That's not an excuse, it was the design.  Python didn't even have nested
scopes (in 2.2's sense) when listcomps were introduced, and it would have
been horribly inconsistent if one and only one binding construct did not
make its target name(s) local wrt to its scope.  Had nested scopes been
there at the time, it would have been more palatable to say that listcomps
define a new scope -- but that would have been a surprise of a different
kind (not to people coming from FP languages, but Python is not an FP
language, and won't become one before Guido's death <wink>).





More information about the Python-list mailing list