[Python-ideas] "maybe import"?

Andrew Barnert abarnert at yahoo.com
Fri Dec 27 21:59:48 CET 2013


On Dec 27, 2013, at 9:43, Ryan Gonzalez <rymg19 at gmail.com> wrote:

> I like that. I usually end up doing that with ElementTree:
> 
> try:
>     from xml.etree import cElementTree as etree

Python 3 has already fixed that. You just import ElementTree and you get the C-accelerated version.

And the same is true for cPickle, etc.

A new language feature that offers a different way to solve something that's already been solved better doesn't seem very useful.

Of course I do write stuff like this when I'm trying to write code that works with 2.6+/3.2+, but a new language feature that's a syntax error in 2.x wouldn't help there either.

> 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.
> _______________________________________________
> 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131227/85579fc3/attachment.html>


More information about the Python-ideas mailing list