Python * 进口 [英] Python * imports

查看:47
本文介绍了Python * 进口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常被告知以下是不好的做法.

I've generally been told that the following is bad practice.

from module import *

主要原因(或者我被告知)是,您可能会导入您不想要的东西,并且它可能会从另一个模块中隐藏一个类似命名的函数或类.

The main reasoning (or so I've been told), is that you could possibly import something you didn't want, and it could shadow a similarly named function or class from another module.

然而,PyQt 怎么样

However, what about PyQt

from PyQt4.QtCore import *

我见过的每个例子都是这样写的,主要是因为从 Qt 导出的所有东西都以Q"开头,所以它不会遮蔽任何东西.

Every example I've ever seen is written this way, mainly because everything exported from Qt starts with "Q", so it's not going to shadow anything.

什么是共识?使用 * 导入是否总是不好?

What's the concensus? Is it always bad to use * imports?

需要说明的是,这个问题专门针对使用 PyQt4.这与我设计其他项目的方式无关.

Just to be clear, this question is specifically in regards to using PyQt4. It has nothing to do with the way I am designing some other project.

基本上,我发现编码到 PEP8 提高了我的代码可读性,除了关于导入 PyQt4,所以我一直无视纯粹主义者的皱眉,直到现在.但是现在我的开发小组正在决定一个约定,我想知道这是否是实用性胜过纯度"的场景,或者我是否应该接受它并处理可怕的 PyQt4 导入

Basically, I've found that coding to PEP8 has improved my code readability, except with regards to importing PyQt4, and so I've disregarded the frowns from purists until now. But now my dev group is deciding on one convention and I'm wondering if this is a scenario "where practicality beats purity", or if I should just suck it up and deal with monstrous PyQt4 imports

from PyQt4.QtGui import QComboBox, QLineEdit, QLayout, Q;lakdfaf.......

推荐答案

这可能会变成一场宗教战争.这取决于您是要明确还是要避免过于冗长.一般来说,遵循 Python 之禅,最好是明确的,但有时人们只是觉得列出来自特定模块的每个导入并不实用.

This can sort of turn into a religious war. It's a matter of whether you want to be explicit or whether you want to avoid being too verbose. In general, following the Zen of Python, it's better to be explicit, but sometimes people just don't find it practical to list every import from particular module.

这篇关于Python * 进口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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