[Python-ideas] Changing semantics of for-loop variable: Alternative version
Greg Ewing
greg.ewing at canterbury.ac.nz
Wed Oct 5 01:15:44 CEST 2011
Ronald Oussoren wrote:
> What worries me with this proposal is that it only affects
> the loop variable, and not other variables. This makes it easy
> to introduce subtle bugs when you forget this.
>
> for val in sequence:
> other = lookup(val)
> result.append(lambda val=val, other=other): doit(val, other))
This is a good point, and I have an alternative version of
the idea that addresses it.
Instead of the cell-replacement behaviour being automatic,
we provide a way to explicity request it, such as
for new i in stuff:
...
The advantage is that can be applied to anything that binds
a name, e.g.
for x in stuff:
new i = 2 * x
def f():
print i
store_away(f)
The disadvantage is that you need to be aware of it and
remember to use it when required. However that's no worse
than the status quo, and I think it would provide a nicer
solution than the default argument hack or any of its
proposed variations.
--
Greg
More information about the Python-ideas
mailing list