Misunderstanding about closures

Hung Jung Lu hungjunglu at yahoo.com
Mon Jun 7 18:27:01 CEST 2004


"Robert Brewer" <fumanchu at amor.org> wrote:
> Alexander May wrote:
> > 
> > >>> l=[]
> > >>> for x in xrange(10):
> > ...   def f():
> > ...     return x
> > ...   l.append(f)
> > ...
> > >>> for f in l:
> > ...     f()
> > ...
> > 9
> > 9
> > 9
> 
> ...because "for" does have it's own scope. The 'x' which is bound in the
> 'for' statement persists in locals(), and it equals 9 after the 'for'
> terminates. Calling each f() simply looks up x in locals() each time;
> there's nothing going on in f() which tells Python that x should be in
> f's scope. That would require an assignment to x within f, usually.

Mostly correct. Except that, in this particular example, the returned
'x' value is pulled from globals() instead of locals(). The locals()
dictionary is empty inside f()'s scope. To see this, run the code:

------------ run in console
l=[]
for x in range(10):
    def f():
        print 'locals', locals().keys()
        print 'globals', globals().keys()
        return x
    l.append(f)
for f in l:
    f()
------------ output
locals []
globals ['x', '__builtins__', '__name__', 'f', '__doc__', 'l']
9
....

regards,

Hung Jung



More information about the Python-list mailing list