2 ms·
> As far as I can tell, the main argument against an import function is aesthetic. Compare: "from random import bar" to "bar = __import__('random').bar" I agre
by dalke 13y ago
> As far as I can tell, the main argument against an import function is aesthetic. Compare: "from random import bar" to "bar = __import__('random').bar"
I agree.
A stronger pro-aesthetic example might be:
>>> from xml.sax.saxutils import escape -vs-
>>> escape = __import__("xml.sax.saxutils").sax.saxutils.escape
As another aesthetic, this is much more complicated with __import__:
>>> from itertools import repeat as _repeat, chain as _chain, starmap as _starmap
and requires an intermediate variable to reduce import resolution overhead.
(That example is from itertools/__init__.py . The ctypes/__init__.py also has a similarly complicated import-as .)