[Python-Dev] just trying to catch up with the semantic

Samuele Pedroni Samuele Pedroni <pedroni@inf.ethz.ch>
Thu, 1 Mar 2001 16:33:14 +0100 (MET)


Hi.

I read the following CVS log from Jeremy:

> Fix core dump in example from Samuele Pedroni:
> 
> from __future__ import nested_scopes
> x=7
> def f():
>     x=1
>     def g():
>         global x
>         def i():
>             def h():
>                 return x
>             return h()
>         return i()
>     return g()
> 
> print f()
> print x
> 
> This kind of code didn't work correctly because x was treated as free
> in i, leading to an attempt to load x in g to make a closure for i.
> 
> Solution is to make global decl apply to nested scopes unless their is
> an assignment.  Thus, x in h is global.
> 

Will that be the intended final semantic?

The more backw-compatible semantic would be for that code to print:
1
7
(I think this was the semantic Guido, was thinking of)

Now, if I have understood well, this prints
7
7

but if I put a x=666 in h this prints:
666
7

but the most natural (just IMHO) nesting semantic would be in that case to
print:
666
666
(so x is considered global despite the assignement, because decl extends to
enclosed scopes too).

I have no preference but I'm confused. Samuele Pedroni.