[Python-checkins] [3.12] gh-71261: Add paragraph on shadowing submodules with star imports (GH-107004) (#107100)

hugovk webhook-mailer at python.org
Sun Jul 23 05:01:28 EDT 2023


https://github.com/python/cpython/commit/a80721b83d6dceffdea9520832f68f0e1bcb82ea
commit: a80721b83d6dceffdea9520832f68f0e1bcb82ea
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: hugovk <hugovk at users.noreply.github.com>
date: 2023-07-23T09:01:24Z
summary:

[3.12] gh-71261: Add paragraph on shadowing submodules with star imports (GH-107004) (#107100)

Co-authored-by: wulmer <wulmer at users.noreply.github.com>

files:
M Doc/tutorial/modules.rst

diff --git a/Doc/tutorial/modules.rst b/Doc/tutorial/modules.rst
index 3bd034bcc9703..734dd1cfe6871 100644
--- a/Doc/tutorial/modules.rst
+++ b/Doc/tutorial/modules.rst
@@ -512,6 +512,22 @@ code::
 This would mean that ``from sound.effects import *`` would import the three
 named submodules of the :mod:`sound.effects` package.
 
+Be aware that submodules might become shadowed by locally defined names. For
+example, if you added a ``reverse`` function to the
+:file:`sound/effects/__init__.py` file, the ``from sound.effects import *``
+would only import the two submodules ``echo`` and ``surround``, but *not* the
+``reverse`` submodule, because it is shadowed by the locally defined
+``reverse`` function::
+
+    __all__ = [
+        "echo",      # refers to the 'echo.py' file
+        "surround",  # refers to the 'surround.py' file
+        "reverse",   # !!! refers to the 'reverse' function now !!!
+    ]
+
+    def reverse(msg: str):  # <-- this name shadows the 'reverse.py' submodule
+        return msg[::-1]    #     in the case of a 'from sound.effects import *'
+
 If ``__all__`` is not defined, the statement ``from sound.effects import *``
 does *not* import all submodules from the package :mod:`sound.effects` into the
 current namespace; it only ensures that the package :mod:`sound.effects` has



More information about the Python-checkins mailing list