[Python-Dev] Relative import

Guido van Rossum guido at python.org
Sun Dec 21 23:48:01 EST 2003


> I like syntax that reads most important left-to-right, so what about
> from MODULE import NAMES as RENAME searching HOW
> or
> import NAMES as RENAME from MODULE searching HOW
> 
> searching could be 'absolute' 'relative' or any of the other
> suggested words.  If you wanted to get fancy it could be a list, if
> it isn't a list people who truly care could cascade try/except on
> ImportError.
> 
> -jackdied
> 
> ps, I like the second version better but it is less like the current version,
> which is a drawback.

This got several encouraging responses, so I have to consider it.

Unfortunately it looks like 'searching' would have to become a
reserved word; if we use the same trick we used to avoid making 'as' a
reserved word, that would be too confusing to decode in the code
generator.  The HOW could be just an identifier then.

I'm not sure how well it'll read in actual examples.  Let's try.
Adapting an example from Opal that was used here before...

  from COGS import generate searching relative

To me, that looks a lot like someone left the commas out of

  from COGS import generate, searching, relative

In general I'm not a big fan of "wordy" clauses like this -- they work
better in languages like SQL or COBOL because those are case-sensitive
so you can use a difference in capitalization there; there the former
would be

  FROM COGS IMPORT generate SEARCHING RELATIVE

(using imaginary syntax) while the latter would be

  FROM COGS IMPORT generate, searching, relative

while in Python the long sequence of lowercase words becomes a blur.

So if the two alternatives are simply 'searching absolute' and
'searching relative', with the default being 'searching absolute' in
Python 3.0 (but the current ambiguous search in Python 2.x) I'd still
prefer making the distinction with "from ...MODULE" vs. "from MODULE"
rather than with a searching clause.  And I still prefer the
single-dot syntax over three dots, because it can be used to spell
importing from the parent or grandparent package explicitly.

A separate suggestion is to switch from "from X import Y" to "import Y
from X".  This seems a gratuitous change even if it reads or edits
marginally better.  Now's not the time to tinker with marginal stuff
-- that should've been done 10 years ago.

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



More information about the Python-Dev mailing list