[Python-Dev] Those import related syntax errors again...

Guido van Rossum guido@digicool.com
Tue, 20 Feb 2001 21:53:03 -0500

> > How about the old fallback to using straight dict lookups when this
> > combination of features is detected?
> I'm posting an opinion on this subject because I'm implementing
> nested scopes in jython.
> It seems that we really should avoid breaking code using import *
> and exec, and to obtain this - I agree - the only way is to fall
> back to some straight dictionary lookup, when both import or exec
> and nested scopes are there
> But doing this AFAIK related to actual python nested scope impl and
> what I'm doing on jython side is quite messy, because we will need
> to keep around "chained" closures as entire dictionaries, because we
> don't know if an exec or import will hide some variable from an
> outer level, or add a new variable that then cannot be interpreted
> as a global one in nested scopes. This is IMO too much heavyweight.
> Another way is to use special rules
> (similar to those for class defs), e.g. having
> <frag>
> y=3
> def f():
>   exec "y=2"
>   def g():
>    return y
>   return g()
> print f()
> </frag>
> # print 3.
> Is that confusing for users? maybe they will more naturally expect 2
> as outcome (given nested scopes).

This seems the best compromise to me.  It will lead to the least
broken code, because this is the behavior that we had before nested
scopes!  It is also quite easy to implement given the current
implementation, I believe.

Maybe we could introduce a warning rather than an error for this
situation though, because even if this behavior is clearly documented,
it will still be confusing to some, so it is better if we outlaw it in
some future version.

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