[Python-ideas] "maybe import"?

Andrew Barnert abarnert at yahoo.com
Sat Dec 28 20:55:19 CET 2013


On Dec 28, 2013, at 4:27, Steven D'Aprano <steve at pearwood.info> wrote:

> On Fri, Dec 27, 2013 at 12:59:48PM -0800, Andrew Barnert wrote:
>> 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.
> 
> 
> Not if the C-accelerated version isn't available.

Right, in which case you get the pure Python version--exactly as if you'd done the try/import/except/import, or used this proposed new feature. So there is no advantage to doing the more complicated thing, except 2.x compatibility--which you obviously wouldn't get with a new language feature.

> Or perhaps there are 
> two pure-Python modules with the same API, and you're not sure which is 
> installed. This issue is bigger than just C-accelerators in the CPython 
> standard library. It applies any time you have two or more alternatives 
> with the same API, a preferred module and an fallback module. E.g. 
> readline in the stdlib, versus pyreadline, a third-party Windows port.

There are maybe three such examples that people use regularly. Is that enough to warrant a change in the language?

Well, that's not quite true. It's very common for modules that were renamed in 3.0, or that were recently added but have backward compat ports on PyPI. But again, a language change won't help those cases anyway. Unless you're expecting another mass renaming in 4.0?


More information about the Python-ideas mailing list