在python中,导入类与导入类模块的优缺点是什么? [英] In python, what are the pros and cons of importing a class vs. importing the class's module?

查看:130
本文介绍了在python中,导入类与导入类模块的优缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个约30名开发人员的团队编写一组python编码指南。作为我的文档的基础,到目前为止,我已经研究了 Google python样式指南 PEP 8样式指南,并包含来自两者的信息。

I'm authoring a set of python coding guidelines for a team of ~30 developers. As a basis for my document, so far I've studied the Google python style guide and the PEP 8 style guide, and incorporated information from both.

Google风格指南比PEP 8更具限制性的一个地方是进口。 Google指南要求开发人员仅导入包和模块,然后通过更合格的名称引用其中的项目。例如:

One place where the Google style guide is more restrictive than PEP 8 is with imports. The Google guide requests developers only import packages and modules only, and then refer to items within by a more-qualified name. For example:

from pkg import module
...
my_class = module.MyClass()

理由是每个标识符的来源以一致的方式表示。对于我们的项目,我们打算使用两个或三个级别的包进行组织,因此要了解标识符的完整来源,读者可能还需要检查import语句。我想提倡这种导入方式作为首选样式:

The justification is that the "source of each identifier is indicated in a consistent way". For our project, we intend to organize with packages two or three levels deep, so to know the full source of the identifier, the reader will likely need to examine the import statement anyway. I'd like to advocate this style of import as a "preferred style":

from pkg.module import MyClass
...
my_class = MyClass()

恕我直言,python构造中的可读性如此当名称更简洁时,列表推导得到改善。

IMHO, the readability in python constructs such as list comprehensions is improved when the names are more succinct.

我不清楚的是python解释器可能在幕后做什么。例如,MyClass现在是该模块的全局命名空间的一部分,还是该模块的所有导入器? (这会很糟糕,可能会导致一些奇怪的错误;​​如果这是真的,我会主张谷歌风格)。

What I'm unclear on is what the python interpreter might do behind the scenes. For example, is MyClass now part of the global namespace for both this module, and all importers of this module? (This would be bad, could lead to some weird bugs; if this were true, I'd advocate the Google style).

我的python开发经验仅限于约6个月(我们的项目专家咨询的人数不多),所以我希望从社区获得更多信息。以下是我已经研究过的一些项目:

My python development experience is limited to about 6 months (and there are not many experts on our project to consult), so I wanted to get more information from the community. Here are some items I've researched already:

effbot - 关于进口的讨论

堆栈溢出 - 导入与导入

python文档 - 模块

python文档 - 导入

感谢您的回复!

推荐答案

在Python中,不存在跨多个模块的全局变量。如果从pkg.module导入MyClass 执行,则 MyClass 位于模块的全局命名空间中,但不包括任何其他模块(包括导入导入MyClass的模块的模块)。

In Python, there is no such thing as a variable that is global across more than one module. If you do from pkg.module import MyClass, then MyClass is in the global namespace of the module where you do that, but not of any other module (including modules that import the module that imports MyClass).

至于更一般的问题,任何导入机制都可以接受,具体取决于情况。如果模块名称很长,您可以通过以不同的名称导入它来缩短:

As for your more general question, either import mechanism can be acceptable depending on the situation. If the module name is long, you can get some shortening by importing it under a different name:

# Awkward
from package import reallylongmodule
reallylongmodule.MyClass()

# Less awkward
from package import reallylongmodule as rlm
rlm.MyClass()

如果类名称足够独特,您可以告诉它来自哪里以及它是什么,那么只导入该类就可以了。但是,如果您有多个模块定义具有相对不同名称的类(例如,处理器,单元,数据,管理器),那么通过模块名称访问它们是个好主意以澄清你正在做什么。

Importing just the class can be okay if the class name is distinctive enough that you can tell where it comes from and what it is. However, if you have multiple modules that define classes with relatively undescriptive names (e.g., "Processor", "Unit", "Data", "Manager"), then it can be a good idea to access them via the module name to clarify what you're doing.

风格指南最终是指南,而不是法律。我自己的偏好是选择一种最大化清晰度和可读性的机制。这需要在避免冗长和繁琐的名称之间进行权衡,同时避免使用简短,模糊或含糊的名称。如何进行权衡取决于您使用的特定库以及您如何使用它们(例如,您导入的模块数量,从它们导入的数量)。

Style guides are ultimately guides and not laws. My own preference would be to choose a mechanism that maximizes clarity and readability. That involves a tradeoff between avoiding long and cumbersome names, and also avoiding short, vague, or cryptic names. How you make that tradeoff depends on the particular libraries you're using and how you're using them (e.g., how many modules you import, how many things you import from them).

这篇关于在python中,导入类与导入类模块的优缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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