[Python-Dev] Explicit Lexical Scoping (pre-PEP?)

Guido van Rossum guido at python.org
Sat Jul 8 05:08:40 CEST 2006


On 7/8/06, skip at pobox.com <skip at pobox.com> wrote:
>
>     Guido> Well, personally I'm for allowing full rebinding semantics but
>     Guido> only when a 'global' (or 'nonlocal') statement is used
>     Guido> first. Making augmented assignment automatically imply 'global'
>     Guido> etc. seems too magical to me.
>
> So, if I understand correctly, in the presence of a global statement search
> just goes up the lexical chain looking for the first occurrence of the
> variable to modify?
>
>     x = 0
>     def f():
>         x = 1
>         def g():
>             global x
>             x = 2
>             print x
>         g()
>         print x
>     f()
>     print x
>
> Today it prints
>
>     2
>     1
>     2
>
> You're suggesting it will print
>
>     2
>     2
>     0
>
> ?

Right. And if the search finds no scope that defines x, it's a
compile-time error (that's also new).

> Sounds reasonable to me.  If we're talking py3k I'd chuck "global" as a
> keyword though and replace it with something like "outer".

This is still under debate. I don't think we ought to change this in
Python 2.x until we've settled on the 3.x syntax and semantics;
eventually (in Python 2.9 or so :-) we can backport it with a
__future__ statement.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list