教程(第4段,https://docs.python.org/3/tutorial/modules.html#importing-from-a-package)提到:
It also includes any submodules of the package that were explicitly loaded by previous import statements. Consider this code:
import sound.effects.echo
import sound.effects.surround
from sound.effects import *
In this example, the echo and surround modules are imported in the current namespace because they are defined in the sound.effects package when the from...import statement is executed. (This also works when
__all__
is defined.)
质疑:
previous import statements
?在from sound.effects import *
不应导入任何内容,除非在包中的__init__.py
的__all__
中定义。在
previous import statements
是指:根据cpython3.6,}中使用符号。
from sound.effects import *
只导入__init__.py
中加载的子模块(通过定义__all__
)。这意味着当__all__
未定义时,不能在echo
和{否则
仅当
__all__
在sound.effects
的__init__.py
中定义类似__all__ = ["echo", "surround", "reverse"]
时有效。在相关问题 更多 >
编程相关推荐