Q: Python 2.0 preliminary features?

Jeremy Hylton jeremy at cnri.reston.va.us
Mon Oct 11 12:33:39 EDT 1999


I don't understand why you need the global declaration in g.  

    def f():
       x = 0
       def g():
          global x
          def h():
            global x
            x = 1

At compile time, you should know which scopes have local variables
named x and which have global declarations for x.  If g does not have
a local variable named x, then the global declaration in h can't
sensibly mean the local variable x in g's scope.

The requirement that there be a global declaration in every
intervening scope seems unnecessary and error-prone.  If you miss one
global statement, you get either weird behavior or exceptions.

This isn't exactly compatible with the current semantics for global,
but it would only cause problems for really obscure code, like this:

    x = 12
    def spam():
        x = 1
        def eggs():
            global x
            y = x - 2

Under current global semantics, the x in eggs would refer to the
global variable x with the value 12.  Under the new semantics, the x
in eggs would refer to the local variable x in spam with the value 1.
So it would break some code, but not very much; only code that uses
multiple nested functions that use the same name for local and global
variables. 

Jeremy






More information about the Python-list mailing list