diff options
author | wulmer <wulmer@users.noreply.github.com> | 2023-07-23 09:00:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-23 09:00:42 (GMT) |
commit | 680f3e1591e406deb04ba44adbef9a5a02395f80 (patch) | |
tree | e46a4e6041539583a59343cec92e26341cf3cc2c | |
parent | 9629d4442e24681e79c1ba93cb92ed2e3b967224 (diff) | |
download | cpython-680f3e1591e406deb04ba44adbef9a5a02395f80.zip cpython-680f3e1591e406deb04ba44adbef9a5a02395f80.tar.gz cpython-680f3e1591e406deb04ba44adbef9a5a02395f80.tar.bz2 |
gh-71261: Add paragraph on shadowing submodules with star imports (#107004)
-rw-r--r-- | Doc/tutorial/modules.rst | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/Doc/tutorial/modules.rst b/Doc/tutorial/modules.rst index 3bd034b..734dd1c 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 |