[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake

Dillon Collins dillonco at comcast.net
Tue Oct 14 13:25:11 CEST 2008

On Tuesday 14 October 2008, Arnaud Delobelle wrote:
> On 14 Oct 2008, at 05:03, Dillon Collins wrote:
> > On Monday 13 October 2008, Arnaud Delobelle wrote:
> >> It's inefficient because it works by deconstructing and
> >> reconstructing
> >> the function bytecode.
> >
> > That's not necessary.  Just make a new closure for it.  Here's some
> > code (I
> > was bored/curious).  The function reclose_kwds is provided for
> > fun.   Enjoy:
> But that doesn't work with global variables, does it?  Globals are the
> reason why I had to scan the bytecode

Nope.  However, I expect that globals would be even easier.  If you want to 
freeze all the variables, you can just replace func_globals with  
func_globals.copy().  Otherwise, you can replace it with some proxy object 
that would read from your dict and fall back to the real globals if 
necessary.  (Probably subclass dict with the __missing__ method.)

Or at least I think that would work.  I don't have time to try it now... 

More information about the Python-ideas mailing list