[Python-ideas] Implicit submodule imports
Steven D'Aprano
steve at pearwood.info
Fri Sep 26 10:15:59 CEST 2014
On Wed, Sep 24, 2014 at 07:57:36PM +0200, Thomas Gläßle wrote:
> Hey folks,
>
> What do you think about making it easier to use packages by
> automatically importing submodules on attribute access.
I think it is a bad idea. And yet I also think that optionally
supporting module.__getattr__ and friends is a good idea.
What's the difference between the two?
(1) "Automatically importing submodules on attribute access" implies
that every package and module does this, whether it is appropriate for
it or not.
(2) Building some sort of support for module.__getattr__ implies
that it is opt-in. Whatever the mechanism ends up being, the module
author has to actively make some sort of __getattr__ hook.
The Zen already has something to say about this:
Explicit is better than implicit.
Automatic importing is implicit importing. Now, of course the Zen is not
an absolute, and modules/packages can preload sub-modules if they so
choose, e.g. os automatically imports os.path. But as a general rule if
you want to import a module, you have to import the module, not it's parent.
> Consider this example:
>
> >>> import matplotlib
> >>> figure = matplotlib.figure.Figure()
> AttributeError: 'module' object has no attribute 'figure'
>
> For the newcomer (like me some months ago) it's not obvious that the
> solution is to import matplotlib.figure.
I sympathise. This issue comes up occasionally on the tutor@ and
python-list at python.org mailing lists. Beginners sometimes don't
understand when they need to do an import and when they dont, so we get
things like `import sys.version`.
In hindsight, it is a little unfortunate that package dotted names and
attribute access use the same notation.
> I'm not sure about potential problems from auto-importing. I currently
> see the following issues:
>
> - harmless looking attribute access can lead to significant code
> execution including side effects. On the other hand, that could always
> be the case.
True, but today it is quite rare that the second line:
import spam
spam.thing
will execute arbitrary code. (The initial import will, of course.)
By making importing automatic, every failed attribute access has to
determine whether or not there is a sub-module to import, which could be
quite expensive.
--
Steven
More information about the Python-ideas
mailing list