Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Doc/tutorial/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down