[Python-Dev] Re: closure semantics

John Williams jrw at pobox.com
Thu Oct 23 18:31:48 EDT 2003


Skip Montanaro wrote:
> 
>     John>    global foo from def
> 
> How do you indicate the particular scope to which foo will be bound (there
> can be many lexically enclosing function definitions)?  Using my example
> again:
> 
>     def outer(a):
>         x = a
>         def inner(a):
>             x = 42
>             def innermost(r):
>                 global x from def       # <--- your notation
>                 x = r
>             print "  inner, x @ start:", x
>             innermost(random.random())
>             print "  inner, x @ end:", x
>         print "outer, x @ start:", x
>         inner(a)
>         print "outer, x @ end:", x
> 
> how do you tell Python that x inside innermost is to be associated with the
> x in inner or the x in outer?

I can think of two reasonable possibilities--either it refers to the 
innermost possible variable, or the compiler rejects this case outright. 
  Either way the problem is easy to solve by renaming one of the variables.

Sorry I wasn't clear--I really only meant to propose a new syntax for 
the already-proposed "global foo in def". For some reason I can't quite 
put my finger on, "in def" looks to me like it's referring to the 
function where the statement occurs, but "from def" looks like it refers 
to some other function.

jw




More information about the Python-Dev mailing list