import*和\uyu all_uuu如何使用子模块导入

2024-05-29 09:45:04 发布

您现在位置:Python中文网/ 问答频道 /正文

教程(第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.)

质疑:

  1. 在第一个语句中,它指的是哪个previous import statements?在
  2. 我不能理解这些陈述和例子。To from sound.effects import *不应导入任何内容,除非在包中的__init__.py__all__中定义。在

Tags: theinfromimportechomodulespackagethis
1条回答
网友
1楼 · 发布于 2024-05-29 09:45:04
  1. previous import statements是指:

    import sound.effects.echo
    import sound.effects.surround
    
  2. 根据cpython3.6,from sound.effects import *只导入__init__.py中加载的子模块(通过定义__all__)。这意味着当__all__未定义时,不能在echo和{}中使用符号。

否则

It also includes any submodules of the package that were explicitly loaded by previous import statements.

仅当__all__sound.effects__init__.py中定义类似__all__ = ["echo", "surround", "reverse"]时有效。在

相关问题 更多 >

    热门问题