[Python-ideas] Idea: Lazy import statement
Greg Ewing
greg.ewing at canterbury.ac.nz
Tue Oct 14 06:56:53 CEST 2008
Josiah Carlson wrote:
> How is this mechanism supposed to behave in the presence of things like...
>
> # baz.py
> from foo lazy import bar
>
> def fcn():
> return bar.obj(...)
I would say "don't do that".
If bar is always used within the baz module, there's no
reason to lazily import it -- just use an ordinary import.
If you're trying to achieve a conditional import, use
an ordinary import that's executed conditionally, e.g.
if moon_is_full:
from foo import bar
frobnicate(bar)
Then py2exe will notice the potential for importing
the foo module and include it.
The only time you should use a lazy import is in the
situation I described, i.e. you're importing things
solely to re-export them for other modules that may
want to use them. Any module that actually uses something
itself, even if only potentially, should use a normal
import.
--
Greg
More information about the Python-ideas
mailing list