[Python-Dev] PEP for Better Control of Nested Lexical Scopes
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu Feb 23 05:25:30 CET 2006
Samuele Pedroni wrote:
> If you are looking for rough edges about nested scopes in Python
> this is probably worse:
>
> >>> x = []
> >>> for i in range(10):
> ... x.append(lambda : i)
> ...
> >>> [y() for y in x]
> [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
As an aside, is there any chance that this could be
changed in 3.0? I.e. have the for-loop create a new
binding for the loop variable on each iteration.
I know Guido seems to be attached to the idea of
being able to use the value of the loop variable
after the loop exits, but I find that to be a dubious
practice readability-wise, and I can't remember ever
using it. There are other ways of getting the same
effect, e.g. assigning it to another variable before
breaking out of the loop, or putting the loop in a
function and using return.
--
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury, | Carpe post meridiam! |
Christchurch, New Zealand | (I'm not a morning person.) |
greg.ewing at canterbury.ac.nz +--------------------------------------+
More information about the Python-Dev
mailing list