Skip to content

Commit 680f3e1

Browse files
authored
gh-71261: Add paragraph on shadowing submodules with star imports (#107004)
1 parent 9629d44 commit 680f3e1

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Doc/tutorial/modules.rst

+16
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,22 @@ code::
512512
This would mean that ``from sound.effects import *`` would import the three
513513
named submodules of the :mod:`sound.effects` package.
514514

515+
Be aware that submodules might become shadowed by locally defined names. For
516+
example, if you added a ``reverse`` function to the
517+
:file:`sound/effects/__init__.py` file, the ``from sound.effects import *``
518+
would only import the two submodules ``echo`` and ``surround``, but *not* the
519+
``reverse`` submodule, because it is shadowed by the locally defined
520+
``reverse`` function::
521+
522+
__all__ = [
523+
"echo", # refers to the 'echo.py' file
524+
"surround", # refers to the 'surround.py' file
525+
"reverse", # !!! refers to the 'reverse' function now !!!
526+
]
527+
528+
def reverse(msg: str): # <-- this name shadows the 'reverse.py' submodule
529+
return msg[::-1] # in the case of a 'from sound.effects import *'
530+
515531
If ``__all__`` is not defined, the statement ``from sound.effects import *``
516532
does *not* import all submodules from the package :mod:`sound.effects` into the
517533
current namespace; it only ensures that the package :mod:`sound.effects` has

0 commit comments

Comments
 (0)