应该避免通配符导入吗? [英] Should wildcard import be avoided?

查看:56
本文介绍了应该避免通配符导入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt 并且遇到了这个问题.如果我的导入语句是:

I'm using PyQt and am running into this issue. If my import statements are:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

然后 pylint 给出了数百个未使用的导入"警告.我很犹豫是否关闭它们,因为可能还有其他未使用的导入实际上很有用.另一种选择是这样做:

then pylint gives hundreds of "Unused import" warnings. I'm hesitant to just turn them off, because there might be other unused imports that are actually useful to see. Another option would be to do this:

from PyQt4.QtCore import Qt, QPointF, QRectF
from PyQt4.QtGui import QGraphicsItem, QGraphicsScene, ...

我最终在 QtGui 线上有 9 个课程.还有第三种选择,那就是:

and I end up having 9 classes on the QtGui line. There's a third option, which is:

from PyQt4 import QtCore, QtGui

然后在我使用它们时使用 QtCore 或 QtGui 作为所有类的前缀.

and then prefix all the classes with QtCore or QtGui whenever I use them.

在这一点上,我不知道我最终在我的项目中做了哪一个,尽管从我的角度来看,最后一个似乎是最痛苦的.这里有哪些常见的做法?使用一种风格是否有技术原因?

At this point I'm agnostic as to which one I end up doing in my project, although the last one seems the most painful from my perspective. What are the common practices here? Are there technical reason to use one style over the other?

推荐答案

您问题标题的答案是是":我建议永远不要使用 from ... import *,并且我讨论了原因在另一个最近的答案中.简而言之,限定名称​​好,裸名非常有限,因此第三个选项"是您提供的最佳名称(因为您将使用限定名称,而不是裸名).

The answer to your question's title is "yes": I recommend never using from ... import *, and I discussed the reasons in another very recent answer. Briefly, qualified names are good, barenames are very limited, so the "third option" is optimal (as you'll be using qualified names, not barenames) among those you present.

(与裸名相比,限定名称的优点包括易于伪造/模拟以进行测试,降低了由意外重新绑定引起的未被注意的错误的风险,能够半伪造"跟踪类"中的顶级名称准确记录您正在使用的内容并简化分析等活动的目的 - 缺点,几乎没有......另请参阅 Python 禅宗中的最后但并非最不重要的公案,import 在交互式解释器提示符处).

(Advantages of qualified names wrt barenames include ease of faking/mocking for testing purposes, reduced to nullified risk of unnoticed errors induced by accidental rebinding, ability to "semi-fake" the top name in a "tracing class" for the purpose of logging exactly what you're using and easing such activities as profiling, and so forth -- disadvantages, just about none... see also the last-but-not-least koan in the Zen of Python, import this at the interactive interpreter prompt).

同样好,如果你对 7 个额外的字符表示不满的话 QtCore.whatever,就是缩写 -- from PyQt4 import QtCore as Crfrom PyQt4将 QtGi 导入为 Gu(然后使用 Cr.blahGu.zorp)等.像所有缩写一样,它是简洁性和清晰性之间的一种权衡(您更愿意将变量命名为 count_of_all_widgets_in_the_inventorynum_widgetsx?通常是中间的选择是最好的,但并非总是如此;-)

Equally good, if you grudge the 7 extra characters to say QtCore.whatever, is to abbreviate -- from PyQt4 import QtCore as Cr and from PyQt4 import QtGi as Gu (then use Cr.blah and Gu.zorp) or the like. Like all abbreviations, it's a style tradeoff between conciseness and clarity (would you rather name a variable count_of_all_widgets_in_the_inventory, num_widgets, or x? often the middle choice would be best, but not always;-).

顺便说一句,我不会在单个 fromimport 语句中使用多个 as 子句(可能会造成混淆),我'宁愿有多个语句(如果任何导入出现问题也更容易调试,如果您将来更改导入,则进行编辑,......).

BTW, I would not use more than one as clause in a single from or import statement (could be confusing), I'd rather have multiple statements (also easier to debug if any import is giving problem, to edit if you change your imports in the future, ...).

这篇关于应该避免通配符导入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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