[Python-Dev] Relative import

Jack Diederich jack at performancedrivers.com
Mon Dec 22 02:52:19 EST 2003

On Sun, Dec 21, 2003 at 08:48:01PM -0800, Guido van Rossum wrote:
> > 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.
> 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.

Too bad the decorator PEP (PEP 318) seems to favor 'as' in the syntax,

import foo [relative] 

is more explicit than the dot syntax and has a nice visual seperator
while avoiding the wordiness problem of 'searching'

> 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

moving the 'import' to always be first would alleviate this a bit
because 'searching' would always follow a non-list.

import generate from COGS searching relative

or if we decorate

import generate from COGS [relative]
from COGS import generate [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
> while in Python the long sequence of lowercase words becomes a blur.

as above, moving import to be first helps but doesn't eliminate the
wordiness problem[*].  'as' and 'from' are short enough to be
easilly seen as keywords, 'find' or 'look' reads less like english but 
would be easier to pick out visually than 'searching'

> So if the two alternatives are simply 'searching absolute' and
> 'searching relative'

Did someone mention 'searching scan' in dusty corner somewhere?
One drawback to the dot syntax is if there can be more than two ways
to do the search.  Adding a third way is easier w/ the searching
clause instead of adding a 'bang' syntax to the 'dot' syntax .  The 
decorator would do one better by being a placeholder for anything that 
modifies the import.

> 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.

Damn, I really liked that part.  I don't get tripped up on 
from X import Y
anymore, but I did when I started w/ python.



[*] I use an emacs hack to display 'lambda' as 'L' to avoid the same
    keyword-reads-like-a-variable effect.

More information about the Python-Dev mailing list