[Python-Dev] nested scopes and global: some corner cases

Samuele Pedroni pedroni@inf.ethz.ch
Sun, 11 Mar 2001 03:11:34 +0100


Hi.

Writing nested scopes support for jython (now it passes test_scope and
test_future <wink>),
I have come across these further corner cases for nested scopes mixed with
global decl,
I have tried them with python 2.1b1 and I wonder if the results are consistent
with
the proposed rule:
a free variable is bound according to the nearest outer scope binding
(assign-like or global decl),
class scopes (for backw-comp) are ignored wrt this.

(I)
from __future__ import nested_scopes

x='top'
def ta():
 global x
 def tata():
  exec "x=1" in locals()
  return x # LOAD_NAME
 return tata

print ta()() prints 1, I believed it should print 'top' and a LOAD_GLOBAL
should have been produced.
In this case the global binding is somehow ignored. Note: putting a global decl
in tata xor removing
the exec make tata deliver 'top' as I expected (LOAD_GLOBALs are emitted).
Is this a bug or I'm missing something?

(II)
from __future__ import nested_scopes

x='top'
def ta():
    x='ta'
    class A:
        global x
        def tata(self):
            return x # LOAD_GLOBAL
    return A

print ta()().tata() # -> 'top'

should not the global decl in class scope be ignored and so x be bound to x in
ta,
resulting in 'ta' as output? If one substitutes global x with x='A' that's what
happens.
Or only local binding in class scope should be ignored but global decl not?

regards, Samuele Pedroni