[Python-ideas] Changing semantics of for-loop variable

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Oct 5 01:20:35 CEST 2011


Jason Orendorff wrote:

> Also: What if the loop variable is also used elsewhere in the function?
> 
>     def f(seq):
>         i = None
>         for i in seq:
>             pass
>         return i
> 
> Does the loop variable get a cell in this case?

No, because i is not referenced from an inner function.
There is no change from the current semantics in this case.

If it *were* referenced from an inner function, then cell
replacement would occur, notwithstanding the fact that
it's used elsewhere in the function.

-- 
Greg



More information about the Python-ideas mailing list