[Python-Dev] PEP for Better Control of Nested Lexical Scopes (original) (raw)

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Feb 24 07:54:14 CET 2006


Thomas Wouters wrote:

On Thu, Feb 23, 2006 at 05:25:30PM +1300, Greg Ewing wrote:

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. You can't do that without introducing a whole new scope

for the body of the

'for' loop,

There's no need for that. The new scope need only include the loop variable -- everything else could still refer to the function's main scope.

There's even a rather elegant way of implementing this in the current CPython. If a nested scope references the loop variable, then it will be in a cell. So you just create a new cell each time round the loop, instead of changing the existing one.

This would even still let you use the value after the loop finished, if that were considered a good idea. But it might be better not to allow that, since it could make alternative implementations difficult.

-- Greg



More information about the Python-Dev mailing list