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

Guido van Rossum guido@digicool.com
Thu, 01 Mar 2001 15:46:56 -0500


> is the semantic (expressed through bytecode instrs) right?

Hi Samuele,

Thanks for bringing this up.  I agree with your predictions for these
examples, and have checked them in as part of the test_scope.py test
suite.  Fortunately Jeremy's code passes the test!

The rule is really pretty simple if you look at it through the right
glasses:

    To resolve a name, search from the inside out for either a scope
    that contains a global statement for that name, or a scope that
    contains a definition for that name (or both).

Thus, on the one hand the effect of a global statement is restricted
to the current scope, excluding nested scopes:

   def f():
       global x
       def g():
           x = 1 # new local

On the other hand, a name mentioned a global hides outer definitions
of the same name, and thus has an effect on nested scopes:

    def f():
       x = 1
       def g():
           global x
           def h():
               return x # global

We shouldn't code like this, but it's good to agree on what it should
mean when encountered!

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