“从X导入a"与“从X导入X"; X.a' [英] 'from X import a' versus 'import X; X.a'

查看:93
本文介绍了“从X导入a"与“从X导入X"; X.a'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一些Python程序员相当一致地使用以下样式(我们将其称为样式1):

I've seen some Python programmers use the following style fairly consistently (we'll call it style 1):

import some_module
# Use some_module.some_identifier in various places.

为此样式的支持,您可以引用>显式优于隐式"格言.我见过其他程序员使用这种样式(样式2):

For support of this style, you can cite the "explicit is better than implicit" maxim. I've seen other programmers use this style (style 2):

from some_module import some_identifier
# Use some_identifier in various places.

我在样式2中看到的主要好处是可维护性-尤其是使用鸭子键入理想情况下,我可能想将some_module交换为some_other_module.我还感觉到样式2赢得了可读性计数" 格言.尽管我倾向于不同意,但人们总是可以争辩说,在使用第一种样式时,搜索和替换同样是一个不错的选择.

The primary benefit that I see in style 2 is maintainability -- especially with duck typing ideals I may want to swap some_module for some_other_module. I also feel style 2 wins points with the "readability counts" maxim. Although I tend to disagree, one can always argue that search-and-replace is just as good an option when using the first style.

附录:注意,可以使用as解决样式1中从some_modulesome_other_module的切换.我忘了提到决定在您的 current 模块中实现some_identifier,这会使创建等效的some_module容器有点尴尬.

Addendum: It was noted that you could use as to solve the switch from some_module to some_other_module in style 1. I forgot to mention that it is also common to decide to implement some_identifier in your current module, which makes creation of an equivalent some_module container slightly awkward.

推荐答案

这两种情况都有用,所以我不认为这是一个非此即彼"的问题. 我会在以下情况下考虑使用模块import x,y,z:

There are uses for both cases, so I don't think this is an either-or issue. I'd consider using from module import x,y,z when:

  • 要导入的东西很少

  • There are a fairly small number of things to import

与模块名称分离时,导入的功能的目的显而易见.如果名称相当通用,则它们可能会与其他名称冲突,并且对您几乎没有帮助.例如.看到remove不会告诉您什么,但是os.remove可能暗示您正在处理文件.

The purpose of the functions imported is obvious when divorced from the module name. If the names are fairly generic, they may clash with others and tell you little. eg. seeing remove tells you little, but os.remove will probably hint that you're dealing with files.

名称不冲突.与上述类似,但更重要. 从不执行以下操作:

The names don't clash. Similar to the above, but more important. Never do something like:

 from os import open

import module [as renamed_module]的优点在于,它提供了有关使用时被调用内容的更多上下文.这样做的缺点是,当模块没有真正提供更多信息时,它会变得更加混乱,并且性能会稍差(2次查找而不是1次查找).

import module [as renamed_module] has the advantage that it gives a bit more context about what is being called when you use it. It has the disadvantage that this is a bit more cluttered when the module isn't really giving more information, and is slightly less performant (2 lookups instead of 1).

然而,它在测试时也具有优势(例如,用模拟对象替换os.open,而不必更改每个模块),并且在使用可变模块时应使用它,例如

It also has advantages when testing however (eg. replacing os.open with a mock object, without having to change every module), and should be used when using mutable modules, e.g.

import config
config.dburl = 'sqlite:///test.db'

如有疑问,我将始终采用import module样式.

If in doubt, I'd always go with the import module style.

这篇关于“从X导入a"与“从X导入X"; X.a'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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