[Python-ideas] "maybe import"?

Steven D'Aprano steve at pearwood.info
Sun Dec 29 00:43:23 CET 2013


On Sat, Dec 28, 2013 at 11:55:19AM -0800, Andrew Barnert wrote:
> On Dec 28, 2013, at 4:27, Steven D'Aprano <steve at pearwood.info> wrote:

> > 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?

There's only one *common* example of augmented assignment, x += 1, but 
that was enough to justify a whole raft of new syntax, += -= *= |= etc
because the *idiom* of "x = x op arg" is common.

What matters is not the specific examples of which modules get used in 
this way, but that the idiom of

try:
    import this
except ImportError:
    import that as this

is common, not the specific values of this/that. I do not expect that 
the idiom of having a fallback module when the preferred module isn't 
available will ever disappear, simply because there are too many 
circumstances in which it comes up.

Having said that, Nick's comments strike me as critical. "import this or 
that" doesn't buy us anything that can't already be solved in other 
ways. If it were easy to add, then perhaps it would be justified. But 
the implementation of import is already gnarly and hard to maintain (so 
I'm told), so adding more complexity to it for something which is purely 
syntactic sugar is probably not the best thing to do.

Maybe in Python 4000 :-)


-- 
Steven


More information about the Python-ideas mailing list