import *

The issue is that the actual names inserted into the scope can only be determined at runtime. I would propose that this only be enabled at the module-level scope to avoid costly lookups and other major complications. Python now makes this assertion too for similar reasons. (See the bottom of http://docs.python.org/ref/import.html.)

Cons

One would not be able to override a builtin function with something of the same name from *. Also, this would disable the compile-time name-error warnings.

Explicit imports of course can be done anywhere.

Comments

DagSverreSeljebotn: Would it be expensive to insert code at module startup to basically try to resolve all builtin functions used towards *-imports, and if they succeed then fail hard?

DagSverreSeljebotn: Actually, a check compile-time can help us here. Look:

(Basically, a way of looking at this is that"from blah import *" is "resolved" compile-time; ie "from numpy import *" is resolved into "from numpy import min, max, round, pi" while compiling based on compile-time knowledge of the library. In fact, if we specify that this is what happens it leaves the way open for compile-time resolving the symbols completely even when star-imported. This is not very clean, but is is a very practical solution which should cover 90% of the cases; and one can always not use *-import if one is compiling in a sandbox or whatever. + big fat warnings if we detect that run-time behaviour would have been different (ie a module *-imported after numpy suddenly also defines pi)).

enhancements/import (last edited 2009-03-01 01:02:24 by localhost)