使用通配符导入的原因是什么? [英] What is the reason for using a wildcard import?

查看:79
本文介绍了使用通配符导入的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习了导入模块,对通配符导入有点困惑.

from module_name import *

我完全不明白使用它的原因,我看到有人说根本不要使用它.

有人能解释一下它的真正含义吗,为什么要使用它?

解决方案

根据 [Python.Docs]:模块 - 关于模块的更多信息(重点是我的):

<块引用>

甚至还有一个变量可以导入模块定义的所有名称:

>>>从 fibo 进口 *>>>纤维蛋白(500)0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

这将导入除以下划线开头的所有名称 (_).在大多数情况下,Python 程序员不使用此功能,因为它在解释器中引入了一组未知的名称,可能隐藏了您已经定义的某些内容.

请注意,从模块或包导入*的做法通常是不受欢迎的,因为它通常会导致代码可读性差.但是,可以使用它来节省交互式会话中的输入.

所以,这意味着:导入all(查看上面的页面,了解__all__ 变量的含义)符号由模块/包导出到当前命名空间.

通常(如上述所述),当一个人在控制台中并希望通过不手动"导入所需的所有内容来节省时间时使用它.
它也被一些不知道要导入什么的人使用(因此他们导入所有内容,因为他们并不真正知道自己在做什么 - 当然也有例外,但这种情况很少见).

无论如何,可能这是最有说服力的例子(因为它依赖于Python):说明它的陷阱:

<块引用>

>>>with open("out.txt", "w") as f:... f.write(不要使用通配符导入!")...27>>>>>>从操作系统导入 *>>>>>>with open("out.txt", "w") as f:... f.write(使用通配符导入...")...回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.类型错误:需要一个整数(得到类型 str)

通配符导入阴影:

作者:

在处理 3rd 方模块时,事情会变得更加混乱(其中碰撞命中率可以呈指数级增长).

I have just learnt about importing modules, and I am a bit confused about the wildcard import.

from module_name import *

I do not understand the reason of using it at all, I see people saying not to use it at all.

Could someone clarify what it really means, and why would one use it?

解决方案

According to [Python.Docs]: Modules - More on Modules (emphasis is mine):

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.

So, it means: importing all (check the above page for the __all__ variable meaning) symbols exported by a module / package into the current namespace.

Generally (as the above states), it's used when one is in the console and wants to save time by not importing everything needed, "manually".
It's also used by some who don't know what to import (so they import everything, since they don't really know what they're doing - there are exceptions of course, but those are rare).

Anyway, probably this is the most eloquent example (as it only relies on Python): illustrating its pitfalls:

>>> 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)

The wildcard import shadows:

by:

Things can get even messier when dealing with 3rd-party modules (where the collision hit chance can grow exponentially).

这篇关于使用通配符导入的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆