模块的Python命名约定

问题:模块的Python命名约定

我有一个模块,其目的是定义一个称为“ nib”的类。(以及一些相关的类。)我应该如何调用模块本身?“笔尖”?“ nibmodule”?还要别的吗?

I have a module whose purpose is to define a class called “nib”. (and a few related classes too.) How should I call the module itself? “nib”? “nibmodule”? Anything else?


回答 0

只是笔尖。将类Nib命名为大写N。有关命名约定和其他样式建议的更多信息,请参见Python样式指南PEP 8

Just nib. Name the class Nib, with a capital N. For more on naming conventions and other style advice, see PEP 8, the Python style guide.


回答 1

我称它为nib.py。我也将其命名为Nib类。

在我正在处理的一个较大的python项目中,我们有许多模块定义了一个重要的类。类以大写字母开头。模块的名称类似于小写的类。这导致如下所示的导入:

from nib import Nib
from foo import Foo
from spam.eggs import Eggs, FriedEggs

有点像模拟Java方式。每个文件一个类。但是,通过增加的灵活性,您可以在合理的情况下始终将另一个类添加到单个文件中。

I would call it nib.py. And I would also name the class Nib.

In a larger python project I’m working on, we have lots of modules defining basically one important class. Classes are named beginning with a capital letter. The modules are named like the class in lowercase. This leads to imports like the following:

from nib import Nib
from foo import Foo
from spam.eggs import Eggs, FriedEggs

It’s a bit like emulating the Java way. One class per file. But with the added flexibility, that you can allways add another class to a single file if it makes sense.


回答 2

我知道我的解决方案从pythonic的角度来看不是很流行,但是我更喜欢使用Java的方法,即一个模块->一个类,并将模块命名为该类。我确实了解python样式背后的原因,但是我不太喜欢包含很多类的非常大的文件。尽管折叠,但我发现很难浏览。

另一个原因是版本控制:拥有大文件意味着您的提交倾向于集中在该文件上。这有可能导致解决大量冲突。您还将丢失提交修改特定文件(因此涉及特定类)的其他日志信息。取而代之的是,您看到了对模块文件的修改,仅带有提交注释以了解已进行了哪些修改。

总结一下,如果您喜欢python原理,请参考其他文章的建议。如果您更喜欢类似Java的哲学,请创建一个包含Nib类的Nib.py。

I know my solution is not very popular from the pythonic point of view, but I prefer to use the Java approach of one module->one class, with the module named as the class. I do understand the reason behind the python style, but I am not too fond of having a very large file containing a lot of classes. I find it difficult to browse, despite folding.

Another reason is version control: having a large file means that your commits tend to concentrate on that file. This can potentially lead to a higher quantity of conflicts to be resolved. You also loose the additional log information that your commit modifies specific files (therefore involving specific classes). Instead you see a modification to the module file, with only the commit comment to understand what modification has been done.

Summing up, if you prefer the python philosophy, go for the suggestions of the other posts. If you instead prefer the java-like philosophy, create a Nib.py containing class Nib.


回答 3

笔尖很好。如有疑问,请参阅Python样式指南。

PEP 8

程序包和模块名称模块应使用简短的全小写名称。如果模块名称可以提高可读性,则可以在模块名称中使用下划线。尽管不鼓励使用下划线,但Python软件包也应使用短小写全名。

由于模块名称已映射到文件名,并且某些文件系统不区分大小写,并且截断了长名称,因此,将模块名称选择为相当短是很重要的-在Unix上这不是问题,但可能是代码传输到较旧的Mac或Windows版本或DOS时出现问题。

当用C或C ++编写的扩展模块具有随附的Python模块提供更高级别(例如,面向对象)的接口时,C / C ++模块具有下划线(例如_socket)。

nib is fine. If in doubt, refer to the Python style guide.

From PEP 8:

Package and Module Names Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short — this won’t be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).


回答 4

PEP-8:程序包和模块名称

模块应使用简短的全小写名称。如果模块名称可以提高可读性,则可以在模块名称中使用下划线。

尽管不鼓励使用下划线,但Python软件包也应使用短小写全名。

当用C或C ++编写的扩展模块具有随附的Python模块提供更高级别(例如,面向对象)的接口时,C / C ++模块具有下划线(例如_socket)。

From PEP-8: Package and Module Names:

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.

Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).


回答 5

python中的foo模块等效于Java中的Foo类文件

要么

python中的foob​​ar模块等同于Java中的FooBar类文件

foo module in python would be the equivalent to a Foo class file in Java

or

foobar module in python would be the equivalent to a FooBar class file in Java