Python 类文件名也应该是驼峰式的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42127593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:16:48  来源:igfitidea点击:

Should Python class filenames also be camelCased?

pythonnaming-conventionsfilenamescamelcasing

提问by m4jesticsun

I know that classes in Python are typically cased using camelCase.

我知道 Python 中的类通常使用驼峰式大小写。

Is it also the normal convention to have the file that contains the class also be camelCase'd especially if the file only contains the class?

将包含类的文件也使用驼峰命名,尤其是在文件只包含类的情况下,这也是正常的约定吗?

For example, should class classNamealso be stored in className.pyinstead of class_name.py?

例如, classclassName也应该存储在className.py而不是class_name.py?

回答by Caitlin Quintero Weaver

The following answer is largely sourced from this answer.

以下答案主要来自此答案

If you're going to follow PEP 8, you should stick to all-lowercase names, with optional underscores.

如果您打算遵循 PEP 8,您应该坚持使用全小写的名称,并带有可选的下划线。

To quote PEP 8's naming conventions for packages & modules:

引用PEP 8 的包和模块命名约定

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

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

And for classes:

对于类:

Class names should normally use the CapWords convention.

类名通常应使用 CapWords 约定。

See this answerfor the difference between a module, class and package:

请参阅此答案以了解模块、类和包之间的区别:

A Python module is simply a Python source file, which can expose classes, functions and global variables.

Python 模块只是一个 Python 源文件,它可以公开类、函数和全局变量。

回答by Hyman

My question is, is it also the normal convention to have the file that contains the class also be camelCase'd especially if the file only contains the class

我的问题是,将包含类的文件也使用驼峰命名是否也是正常的约定,特别是如果文件只包含类

Short answer: No.

简短的回答:没有。

Longer answer: should be all lower case and underscores as needed.

更长的答案:应根据需要全部使用小写和下划线。

From PEP8 "Package and Module Names":

来自PEP8“包和模块名称”

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.

模块应该有简短的全小写名称。如果可以提高可读性,可以在模块名称中使用下划线。Python 包也应该有简短的、全小写的名称,但不鼓励使用下划线。

If you're unclear what a moduleis:

如果您不清楚模块是什么:

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

模块是包含 Python 定义和语句的文件。文件名是后缀 .py 的模块名称。

回答by BuvinJ

The official convention is to use all lower case for file names (as others have already stated). The reason, however, has not been mentioned...

官方约定是对文件名使用全部小写(正如其他人已经说明的那样)。然而,原因并没有被提及......

Since Python works cross platform (and it is common to use it in that manner), but file systems vary in the use of casing, it is better to just eliminate alternate cases. In Linux, for instance, it is possible to have MyClass.py and myclass.py in the same directory. That is not so in Windows!

由于 Python 跨平台工作(并且以这种方式使用它很常见),但是文件系统在大小写的使用方面有所不同,因此最好消除替代情况。例如,在 Linux 中,可以将 MyClass.py 和 myclass.py 放在同一目录中。在 Windows 中并非如此!

On a related note, if you have MyClass.py and myclass.py in a git repo, or even just change the casing on the same file, git can act funky when you push/pull across from Linux and Windows.

在相关说明中,如果您在 git 存储库中有 MyClass.py 和 myclass.py,或者甚至只是更改同一个文件的大小写,当您从 Linux 和 Windows 推/拉时,git 可能会表现得很时髦。

And, while barely on topic, but in the same vein, SQL has these same issues where different standards and configurations may or may not allow UpperCases on table names.

而且,虽然几乎没有主题,但同样,SQL 也有这些相同的问题,不同的标准和配置可能允许也可能不允许表名上的大写。

I, personally, find it more pleasant to read TitleCasing / camelCasing even on filenames, but when you do anything that can work cross platform it's safest not to.

就我个人而言,即使在文件名上阅读 TitleCasing / camelCasing 也更令人愉快,但是当你做任何可以跨平台工作的事情时,最好不要这样做。

回答by Franz Felberer

There is a difference in the naming convention of the class name and the file that contains this class. This missunderstanding might come from languages like java where it is common to have one file per class.

类名和包含此类的文件的命名约定存在差异。这种误解可能来自像 java 这样的语言,其中每个类都有一个文件是很常见的。

In python you can have several classes per modul (a simple .py file). The classes in this module/file should be called according to the class naming convention: Class names should normally use the CapWords convention. The file containing this classes should follow the modul naming convention: Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.

在 python 中,每个模块可以有多个类(一个简单的 .py 文件)。应根据类命名约定调用此模块/文件中的类:类名称通常应使用 CapWords 约定。包含此类的文件应遵循模块命名约定:模块应具有简短的全小写名称。如果可以提高可读性,可以在模块名称中使用下划线。

=> CamelCase should in the file camelcase.py (or camel_case.py if neccessary)

=> CamelCase 应该在文件camelcase.py(或camel_case.py,如果需要)