[Python-ideas] import fallback syntax

Guido van Rossum guido at python.org
Thu Mar 8 19:03:34 CET 2012


We used to have a lot more of these. The agreed-upon solution is to
have this hidden inside (for instance) the decimal module. E.g. if you
import heapq, you get heapq.py which attempts to import _heapq but
gives you the Python implementation if that fails.

--Guido

On Thu, Mar 8, 2012 at 9:55 AM, Chris Withers <chris at simplistix.co.uk> wrote:
> Hi All,
>
> I see a lot of Python like this:
>
> try:
>  from cDecimal import Decimal
> except ImportError:
>  from decimal import Decimal
>
> ...and nest deeper if you're talking about ElementTree.
>
> How about some syntactical sugar to make this less obnoxious:
>
> from cDecimal or decimal import Decimal
>
> from x.y.z import X as X or
>     a.b.z import Y as X or
>
> what do people think?
>
> Chris
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
>            - http://www.simplistix.co.uk
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas



-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list