使用通配符导入的原因是什么?
What is the reason for using a wildcard import?
刚学导入模块,对通配符导入有点迷茫
from module_name import *
我完全不明白为什么要用它,我看到有人说根本不用它。
谁能解释一下它的真正含义,为什么要使用它?
这用于从模块导入所有内容。建议您不要使用它的原因是因为它可能会混淆您正在使用的函数或 class 的来源。而且,有些东西在不同的模块中可能重名,这样导入会覆盖之前导入的。
from module import *
通常从给定模块导入每个名称(尽管模块可能使用 __all__
来限制它)。通常最好避免这样做,因为这组名称可能会随着时间的推移而改变,可能会改变您的代码可用的名称。
不过,为了方便起见,我有时会在交互式会话中使用它。
根据[Python.Docs]: Modules - More on Modules(强调是我的):
There is even a variant to import all names that a module defines:
>>> from fibo import *
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This imports all names except those beginning with an underscore (_
). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.
Note that in general the practice of importing *
from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
因此,这意味着:导入 all(查看上面的页面以了解 __all__ 变量含义) symbols 由模块/包导出到 当前命名空间 .
通常(如上所述),当一个人在控制台中并希望通过不“手动”导入所需的所有内容来节省时间时使用它。
它也被一些不知道导入什么的人使用(所以他们导入所有东西,因为他们并不真正知道他们在做什么 - 当然也有例外,但这种情况很少见)。
无论如何,可能这是最多的eloquent示例(因为它仅 依赖于 Python):说明它的陷阱:
>>> with open("out.txt", "w") as f:
... f.write("DON'T USE wildcard imports!")
...
27
>>>
>>> from os import *
>>>
>>> with open("out.txt", "w") as f:
... f.write("USING wildcard imports ...")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
通配符导入阴影:
作者:
处理 3rd 方模块时,事情会变得更加混乱(碰撞命中率会呈指数增长)。
刚学导入模块,对通配符导入有点迷茫
from module_name import *
我完全不明白为什么要用它,我看到有人说根本不用它。
谁能解释一下它的真正含义,为什么要使用它?
这用于从模块导入所有内容。建议您不要使用它的原因是因为它可能会混淆您正在使用的函数或 class 的来源。而且,有些东西在不同的模块中可能重名,这样导入会覆盖之前导入的。
from module import *
通常从给定模块导入每个名称(尽管模块可能使用 __all__
来限制它)。通常最好避免这样做,因为这组名称可能会随着时间的推移而改变,可能会改变您的代码可用的名称。
不过,为了方便起见,我有时会在交互式会话中使用它。
根据[Python.Docs]: Modules - More on Modules(强调是我的):
There is even a variant to import all names that a module defines:
>>> from fibo import * >>> fib(500) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This imports all names except those beginning with an underscore (
_
). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.Note that in general the practice of importing
*
from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
因此,这意味着:导入 all(查看上面的页面以了解 __all__ 变量含义) symbols 由模块/包导出到 当前命名空间 .
通常(如上所述),当一个人在控制台中并希望通过不“手动”导入所需的所有内容来节省时间时使用它。
它也被一些不知道导入什么的人使用(所以他们导入所有东西,因为他们并不真正知道他们在做什么 - 当然也有例外,但这种情况很少见)。
无论如何,可能这是最多的eloquent示例(因为它仅 依赖于 Python):说明它的陷阱:
>>> with open("out.txt", "w") as f: ... f.write("DON'T USE wildcard imports!") ... 27 >>> >>> from os import * >>> >>> with open("out.txt", "w") as f: ... f.write("USING wildcard imports ...") ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: an integer is required (got type str)
通配符导入阴影:
作者:
处理 3rd 方模块时,事情会变得更加混乱(碰撞命中率会呈指数增长)。