[Python-Dev] closure semantics

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Fri Oct 24 04:11:01 EDT 2003


> From: Moore, Paul [mailto:Paul.Moore at atosorigin.com]
> > From: Delaney, Timothy C (Timothy) [mailto:tdelaney at avaya.com]
> 
> >    global <name> [in <scope>]
> >
> > where <scope> default to the current module.
> 
> This made me think. What should be the effect of
> 
>     def f():
>         x = 12
>         def g():
>             global y in f
>             y = 12
>         g()
>         print locals()
> 
> I suspect the answer is "it's illegal". But by extension from 
> the current
> behaviour of "global", it should create a local variable in f.

My understanding of (all) the proposals, and what I would expect, is identical semantics to the current 'global', but the affected scope.

So yes, the above should create a local name `y` in `f`. The local name `y` would be allocated at compile time, just like any other local name.

Likewise, the following should be illegal:

     def f():
         x = 12
         y = 1
         def g():
             global y in f
             y = 12
         g()
         print locals()

because the global statement occurs after a local binding of the name.

Tim Delaney



More information about the Python-Dev mailing list