Association between naming classes and naming their files in python (convention?)

给你一囗甜甜゛ 提交于 2019-11-30 02:38:25

What you have presented is the standard convention.

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).

Class Names

Almost without exception, class names use the CapWords convention. Classes for internal use have a leading underscore in addition.

(Python Style Guide)


See e.g.

from configparser import ConfigParser

(which, incidentally, was ConfigParser in Python 2.x but changed to be lowercase in 3.x).

PEP 8 says:

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.

I'll also note that you shouldn't necessarily have on only one class per file. Rather you should include related classes together in the same file. (Of course in some cases, having one class to a file works, but that is not always the case)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!