[Python-Dev] rethinking import-related syntax errors
Jeremy Hylton
jeremy@alum.mit.edu
Wed, 31 Jan 2001 19:36:11 -0500 (EST)
I'd like to summarize the thread prompted by the compiler changes that
implemented long-stated restrictions in the ref manual and ask a
related question about backwards compatibility.
The two changes were:
1. If a name is declared global in a function scope, it is an error
to import with that name as a target. Example:
def foo():
global string
import string # error
2. It is illegal to use 'from ... import *' in a function. Example:
def foo():
from string import *
I believe Guido's recommendation about these two rules are:
1. Allow it, even though it dodgy style. A two-stager would be
clearer:
def foo():
global string
import string as string_mod
string = string_mod
2. Keep the restriction, because it's really bad style. It can
also cause subtle problems with nested scopes. Example:
def f():
from string import *
def g():
return strip
....
It might be reasonable to expect that strip would refer to the
binding introduced by "from string import *" but there is no
reasonable way to support this.
The other issue raised was the two extra arguments to new.code().
I'll move those to the end and make them optional.
The related question is whether I should worry about backwards
compatibility at the C level. PyFrame_New(), PyFunction_New(), and
PyCode_New() all have different signatures. Should I do anything
about this?
Jeremy