summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2015-04-22 22:38:26 (GMT)
committerBarry Warsaw <barry@python.org>2015-04-22 22:38:26 (GMT)
commitb5a3d9bebb3c0019ef8464a6a0962e09b9b4b82d (patch)
tree74ecd0d96af59922c901ca031dd6bc69f4bc757d /Doc
parent532af16346de1e136ff5894699fd46dc73450675 (diff)
parent4e1f355c0e90b7f7ebf05643a7ff5b75c5fb45ef (diff)
downloadcpython-b5a3d9bebb3c0019ef8464a6a0962e09b9b4b82d.zip
cpython-b5a3d9bebb3c0019ef8464a6a0962e09b9b4b82d.tar.gz
cpython-b5a3d9bebb3c0019ef8464a6a0962e09b9b4b82d.tar.bz2
Issue #24029: Document the name binding behavior for submodule imports.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/reference/import.rst35
1 files changed, 35 insertions, 0 deletions
diff --git a/Doc/reference/import.rst b/Doc/reference/import.rst
index dec5f8f..50e4688 100644
--- a/Doc/reference/import.rst
+++ b/Doc/reference/import.rst
@@ -468,6 +468,41 @@ import machinery will create the new module itself.
``create_module()`` is not. Starting in Python 3.6 it will be an error to not
define ``create_module()`` on a loader attached to a ModuleSpec.
+Submodules
+----------
+
+When a submodule is loaded using any mechanism (e.g. ``importlib`` APIs, the
+``import`` or ``import-from`` statements, or built-in ``__import__()``) a
+binding is placed in the parent module's namespace to the submodule object.
+For example, if package ``spam`` has a submodule ``foo``, after importing
+``spam.foo``, ``spam`` will have an attribute ``foo`` which is bound to the
+submodule. Let's say you have the following directory structure::
+
+ spam/
+ __init__.py
+ foo.py
+ bar.py
+
+and ``spam/__init__.py`` has the following lines in it::
+
+ from .foo import Foo
+ from .bar import Bar
+
+then executing the following puts a name binding to ``foo`` and ``bar`` in the
+``spam`` module::
+
+ >>> import spam
+ >>> spam.foo
+ <module 'spam.foo' from '/tmp/imports/spam/foo.py'>
+ >>> spam.bar
+ <module 'spam.bar' from '/tmp/imports/spam/bar.py'>
+
+Given Python's familiar name binding rules this might seem surprising, but
+it's actually a fundamental feature of the import system. The invariant
+holding is that if you have ``sys.modules['spam']`` and
+``sys.modules['spam.foo']`` (as you would after the above import), the latter
+must appear as the ``foo`` attribute of the former.
+
Module spec
-----------