为什么使用 from module import * 是不好的做法?
Why is it bad practice to use from module import *?
作为'habit',Whosebug 和示例页面中几乎每个人都使用import numpy as np
然后输入
t = numpy.arange(0,40000,4000)
为什么不 we/Why 使用 from numpy import *
是不好的做法吗
然后输入
t = arange(0,40000,4000)
请给我理由。
(我猜:
1. 如果我们需要导入多个模块,不同模块中的一些函数共享相同的名称。
2. 在 import module == from module import *?,我可以看到这个 'habit' 导致处理时间更快。)还有哪些其他原因?
Python style guide 是这样说的:
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).
作为'habit',Whosebug 和示例页面中几乎每个人都使用import numpy as np
然后输入
t = numpy.arange(0,40000,4000)
为什么不 we/Why 使用 from numpy import *
是不好的做法吗
然后输入
t = arange(0,40000,4000)
请给我理由。 (我猜: 1. 如果我们需要导入多个模块,不同模块中的一些函数共享相同的名称。 2. 在 import module == from module import *?,我可以看到这个 'habit' 导致处理时间更快。)还有哪些其他原因?
Python style guide 是这样说的:
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).