[issue24029] Surprising name binding behavior of submodule imports needs documenting

Barry A. Warsaw report at bugs.python.org
Wed Apr 22 19:20:15 CEST 2015


Barry A. Warsaw added the comment:

>From Guido:

It's definitely intentional, and it's fundamental to the package import
design. We've had many implementations of package import (remember "ni.py"?
last seen as "knee.py") and it was always there, because this is done as
part of *submodule loading*. For better or for worse (and because I didn't
know Java at the time :-) Python declares that if you write `import
foo.bar` then later in your code you can use `foo.bar` to reference to the
bar submodule of package foo. And the way this is done is to make each
submodule an attribute of its parent package. This is done when the
submodule is first loaded, and because of the strict separation between
loading and importing, it is done no matter what form of import was used to
load bar.

I guess another thing to realize is that the globals of __init__.py are
also the attribute namespace of the package.

I'm not surprised it's in the reference manual -- that hasn't been updated
thoroughly in ages, and I sometimes cry when I see it. :-) So please do
clarify this for the benefit of future implementers.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24029>
_______________________________________


More information about the Python-bugs-list mailing list