“从模块导入 *” VS “导入模块”
“from module import *” VS “import module”
from module import *
VS import module
我所知道的
我知道两者之间的区别,区别在于当你使用 from module import *
时,你可以直接引用模块中的 类、函数等,就像它们在文件,它们是自己导入的。
但是当你只是使用import module
时,你必须在对象名称前使用module.
来引用它。
问题
所以我不知道为什么有时使用 from module import *
而不是 import module
被认为是不好的做法?
PEP 8 表示
Wildcard imports (from <module> import *
) should be avoided, as they
make it unclear which names are present in the namespace, confusing
both readers and many automated tools. There is one defensible use
case for a wildcard import, which is to republish an internal
interface as part of a public API (for example, overwriting a pure
Python implementation of an interface with the definitions from an
optional accelerator module and exactly which definitions will be
overwritten isn't known in advance).
from module import *
VS import module
我所知道的
我知道两者之间的区别,区别在于当你使用 from module import *
时,你可以直接引用模块中的 类、函数等,就像它们在文件,它们是自己导入的。
但是当你只是使用import module
时,你必须在对象名称前使用module.
来引用它。
问题
所以我不知道为什么有时使用 from module import *
而不是 import module
被认为是不好的做法?
PEP 8 表示
Wildcard imports (
from <module> import *
) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).