[Python-ideas] "maybe import"?

Ryan Gonzalez rymg19 at gmail.com
Fri Dec 27 18:43:42 CET 2013


I like that. I usually end up doing that with ElementTree:

try:
    from xml.etree import cElementTree as etree
except:
    try:
        from lxml import etree
    except:
        from xml.etree import ElementTree as etree

It would all be:

import xml.etree.cElementTree or lxml.etree or xml.etree.ElementTree as
etree



On Thu, Dec 26, 2013 at 10:36 PM, Amber Yust <amber.yust at gmail.com> wrote:

> On Thu Dec 26 2013 at 7:40:19 PM, Bruce Leban <bruce at leapyear.org> wrote:
>
>> I think you mean do the import if the name is unbound *or bound to None.* Otherwise,
>> it doesn't work in the example you gave.
>>
>
> Yes, that is what I meant, sorry. Another option would be "not bound to
> something that is not a module" - but I think "unbound or None" is probably
> the most versatile option.
>
> On Thu Dec 26 2013 at 8:08:45 PM, Steven D'Aprano <steve at pearwood.info>
> wrote:
>
>> I'm not (yet) convinced of the need for this functionality, but if
>> Python did gain this, I think I would prefer the colour of this
>> bike-shed to be "perhaps import" rather than "maybe import".
>
>
> Another option would be to re-use the 'or' keyword:
>
>     from foo import bar or None
>
> Where the bit after the 'or' simply specifies a default value to assign if
> an ImportError occurs.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Ryan
When your hammer is C++, everything begins to look like a thumb.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131227/b8cfb46b/attachment.html>


More information about the Python-ideas mailing list