[Python-Dev] Syntax suggestion for imports

Christian Heimes lists at cheimes.de
Thu Jan 3 15:20:53 CET 2008


Raymond Hettinger wrote:
 > How about a new, simpler syntax:
> 
> * import threading or dummy_threading as threading
> 
> * import xml.etree.CElementTree or cElementTree or elementree.ElementTree as ET
> 
> * from cStringIO or StringIO import StringIO
> 
> * import readline or emptymodule

The syntax idea has a nice ring to it, except for the last idea. As
others have already said, the name emptymodule is too magic.

The readline example becomes more readable when you change the import to

import readline or None as readline


In my opinion the import or as syntax definition is easy to understand
if you force the user to always have an "as" statement. The None name is
optional but must be the last name:

import name[, or name2[, or name3 ...] [, or None] as target

This translates into:

try:
    import name as target
except ImportError:
    try:
        import name2 as target
    except ImportError:
        target = None

from name[, or name2 ...] [, or None] import target

translates into

try:
    from name import target
except ImportError:
    try:
        from name2 import target
    except ImportError:
        target = None

Christian


More information about the Python-Dev mailing list