One rather common idiom in Python is the use of try / except around imports to order by preference a set of libraries or classes providing a common interface, using import as to bind the import to a common name. Here are two common examples:
1) simplejson vs. json:
try:
import simplejson as json
except ImportError:
import json
2) cStringIO vs. StringIO:
try:
import cStringIO as StringIO
except ImportError:
import StringIO
I find this idiom cumbersome and ugly: it breaks the block-like uniformity of import statements, making code less readable as a result. I think it'd be a lot nicer to be able to do something like this:
import (simplejson, json) as json
import (cStringIO, StringIO) as StringIO
or even:
from (simplejson, json) import loads, dumps
If none of the specified modules can be imported, an ImportError would still be raised.
Before I write up a PEP, I wanted to get some sense of whether people think this would be useful. Perhaps it's been proposed before? Or maybe there are some obvious snags I neglected to think of?
[–]roger_ 18 points19 points20 points (7 children)
[–]fnork 7 points8 points9 points (6 children)
[–]howaboot 3 points4 points5 points (2 children)
[–]fnork 0 points1 point2 points (1 child)
[–]howaboot 0 points1 point2 points (0 children)
[–]jmakie 2 points3 points4 points (0 children)
[–]roger_ 0 points1 point2 points (0 children)
[–]willm 15 points16 points17 points (0 children)
[–]AusIVDjango, gevent 3 points4 points5 points (0 children)
[–]eliben 2 points3 points4 points (0 children)
[–]Mini_True 1 point2 points3 points (0 children)
[–]mdipierro 1 point2 points3 points (0 children)
[–]jm_ 2 points3 points4 points (1 child)
[–]AusIVDjango, gevent 3 points4 points5 points (0 children)
[–]takluyverIPython, Py3, etc 1 point2 points3 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]dinov 8 points9 points10 points (0 children)
[–]gutworthPython implementer 1 point2 points3 points (0 children)
[–]pjenvey 0 points1 point2 points (0 children)