
On Thu, 22 Feb 2001, Ka-Ping Yee wrote:
- So what's the issue with not being able to determine which variable binds in which scope? With the model just described, it's perfectly clear. Is all this breakage only caused by the particular optimizations for lookup in the implementation (fast locals, etc.)? Or have i missed something obvious?
That was poorly phrased. To clarify, i am making the assumption that the compiler wants each name to be associated with exactly one scope per block in which it appears. 1. Is the assumption true? 2. If so, is this constraint motivated only by lookup optimization? 3. Why enforce this constraint when it would be inconsistent with behaviour that we already have at the top level? If foo.py contains "x = 1", then this works at the top level: >>> if 1: # top level ... x = 3 ... print x ... from foo import * ... def g(): print x ... g() ... 3 1 I am suggesting that it should do exactly the same thing in a function: >>> def f(): # x = 3 inside, no g() ... x = 3 ... print x ... from foo import * ... print x ... >>> f() 3 1 >>> def f(): # x = 3 inside, nested g() ... x = 3 ... print x ... from foo import * ... def g(): print x ... g() ... >>> f() 3 1 >>> x = 3 >>> def f(): # x = 3 outside, nested g() ... print x ... from foo import * ... def g(): print x ... g() ... >>> f() 3 1 (Replacing "from foo import *" above with "x = 1" or "exec('x = 1')" should make no difference. So this isn't just about internal-import-* and exec-without-in, even if we do eventually deprecate internal-import-* and exec-without-in -- which i would tend to support.) Here is a summary of the behaviour i observe and propose. 1.5.2 2.1a1 suggested top level from foo import * 3,1 3,1 3,1 exec('x = 1') 3,1 3,1 3,1 x = 1 3,1 3,1 3,1 x = 3 outside, no g() from foo import * 3,1 3,1 3,1 exec('x = 1') 3,1 3,1 3,1 x = 1 x UnboundLocal 3,1 x = 3 inside, no g() from foo import * 3,1 3,1 3,1 exec('x = 1') 3,1 3,1 3,1 x = 1 x UnboundLocal 3,1 x = 3 outside, nested g() from foo import * 3,3 SyntaxError 3,1 exec('x = 1') 3,3 SyntaxError 3,1 x = 1 x UnboundLocal 3,1 x = 3 inside, nested g() from foo import * 3,x SyntaxError 3,1 exec('x = 1') 3,x SyntaxError 3,1 x = 1 3,x 3,1 3,1 (I don't know what the heck is going on in Python 1.5.2 in the cases where it prints 'x'.) My postulates are: 1. "exec('x = 1')" should behave exactly the same as "x = 1" 2. "from foo import *" should do the same as "x = 1" 3. "def g(): print x" should behave the same as "print x" The testing script is attached. -- ?!ng