need design suggestions: local namespace problem

Nathaniel Gray n8gray at caltech.edu.is.my.e-mail.address.com
Tue Oct 2 20:29:46 EDT 2001


Jason R. Mastaler wrote:
> I was thinking of splitting Defaults.py up into multiple files
> (Qmail.py and Sendmail.py), and then using local namespace trickery so
> that within the program main I can do things like:
> 
>   if qmail:
>      import Qmail as mta
>   elif sendmail:
>      import Sendmail as mta
> 
>   sys.exit(mta.ERR_HARD)
> 
> However, the "import module as name" syntax is a Python2-ism, and I'd
> still like to support Python-1.5.2 if I can.

No problemo:
"""
if qmail:
        import Qmail
        mta = Qmail
elif sendmail:
        import Sendmail
        mta = Sendmail
"""

This is almost exactly what "import a as b" is syntactic sugar for.  (To be 
exact you would have to del Qmail and Sendmail)

Have fun,
-n8

-- 
Nathaniel Gray

California Institute of Technology
Computation and Neural Systems
--




More information about the Python-list mailing list