Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2097f53

Browse files
committed
Issue #24029: Document the name binding behavior for submodule imports.
1 parent 34e0060 commit 2097f53

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Doc/reference/import.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,41 @@ import machinery will create the new module itself.
461461
into :data:`sys.modules`, but it must remove **only** the failing
462462
module, and only if the loader itself has loaded it explicitly.
463463

464+
Submodules
465+
----------
466+
467+
When a submodule is loaded using any mechanism (e.g. ``importlib`` APIs, the
468+
``import`` or ``import-from`` statements, or built-in ``__import__()``) a
469+
binding is placed in the parent module's namespace to the submodule object.
470+
For example, if package ``spam`` has a submodule ``foo``, after importing
471+
``spam.foo``, ``spam`` will have an attribute ``foo`` which is bound to the
472+
submodule. Let's say you have the following directory structure::
473+
474+
spam/
475+
__init__.py
476+
foo.py
477+
bar.py
478+
479+
and ``spam/__init__.py`` has the following lines in it::
480+
481+
from .foo import Foo
482+
from .bar import Bar
483+
484+
then executing the following puts a name binding to ``foo`` and ``bar`` in the
485+
``spam`` module::
486+
487+
>>> import spam
488+
>>> spam.foo
489+
<module 'spam.foo' from '/tmp/imports/spam/foo.py'>
490+
>>> spam.bar
491+
<module 'spam.bar' from '/tmp/imports/spam/bar.py'>
492+
493+
Given Python's familiar name binding rules this might seem surprising, but
494+
it's actually a fundamental feature of the import system. The invariant
495+
holding is that if you have ``sys.modules['spam']`` and
496+
``sys.modules['spam.foo']`` (as you would after the above import), the latter
497+
must appear as the ``foo`` attribute of the former.
498+
464499
Module spec
465500
-----------
466501

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ C API
220220

221221
- Issue #23998: PyImport_ReInitLock() now checks for lock allocation error
222222

223+
Documentation
224+
-------------
225+
226+
- Issue #24029: Document the name binding behavior for submodule imports.
227+
223228

224229
What's New in Python 3.4.3?
225230
===========================

0 commit comments

Comments
 (0)