Python class 文件名也应该是驼峰式的吗?
Should Python class filenames also be camelCased?
我知道 Python 中的 classes 通常使用驼峰式大小写。
让包含 class 的文件也采用驼峰式命名是否也是正常惯例,尤其是当文件仅包含 class 时?
例如,class className
是否也应该存储在 className.py
而不是 class_name.py
?
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
简答:否
更长的答案:应根据需要全部小写和下划线。
来自PEP8 "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.
如果您不清楚 module 是什么:
A module is a file containing Python definitions and statements. The
file name is the module name with the suffix .py appended.
以下答案主要来自this answer。
如果您要遵循 PEP 8,则应坚持使用 all-lowercase 名称,下划线可选。
引用PEP 8's naming conventions for packages & modules:
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
对于 classes:
Class names should normally use the CapWords convention.
请参阅this answer了解模块、class和包之间的区别:
A Python module is simply a Python source file, which can expose classes, functions and global variables.
官方惯例是文件名全部小写(其他人已经说过)。然而,没有提到原因...
由于 Python 跨平台工作(并且以这种方式使用它很常见),但文件系统在大小写的使用上有所不同,最好只消除替代情况。例如,在 Linux 中,可以在同一目录中包含 MyClass.py 和 myclass.py。在 Windows!
中并非如此
相关说明,如果您在 git 存储库中有 MyClass.py 和 myclass.py,或者甚至只是更改同一文件的大小写,git 可以当你 push/pull 在 Linux 和 Windows 对面时表现得很时髦。
而且,虽然几乎没有主题,但本着同样的精神,SQL 也有这些相同的问题,不同的标准和配置可能会或可能不会允许 table 名称上的大写。
就我个人而言,即使在文件名上阅读 TitleCasing / camelCasing 也更令人愉快,但是当您做任何可以跨平台工作的事情时,最好不要这样做。
class 名称的命名约定与包含此 class 的文件不同。这种误解可能来自 java 这样的语言,在这种语言中,每个 class.
有一个文件是很常见的
在 python 中,每个模块(一个简单的 .py 文件)可以有几个 classes。此 module/file 中的 classes 应根据 class 命名约定进行调用:Class 名称通常应使用 CapWords 约定。
包含此 classes 的文件应遵循模块命名约定:模块应具有简短的全小写名称。如果提高可读性,可以在模块名称中使用下划线。
=> CamelCase 应该在文件中 camelcase.py(或者 camel_case.py 如果需要的话)
我知道 Python 中的 classes 通常使用驼峰式大小写。
让包含 class 的文件也采用驼峰式命名是否也是正常惯例,尤其是当文件仅包含 class 时?
例如,class className
是否也应该存储在 className.py
而不是 class_name.py
?
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
简答:否
更长的答案:应根据需要全部小写和下划线。
来自PEP8 "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.
如果您不清楚 module 是什么:
A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.
以下答案主要来自this answer。
如果您要遵循 PEP 8,则应坚持使用 all-lowercase 名称,下划线可选。
引用PEP 8's naming conventions for packages & modules:
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
对于 classes:
Class names should normally use the CapWords convention.
请参阅this answer了解模块、class和包之间的区别:
A Python module is simply a Python source file, which can expose classes, functions and global variables.
官方惯例是文件名全部小写(其他人已经说过)。然而,没有提到原因...
由于 Python 跨平台工作(并且以这种方式使用它很常见),但文件系统在大小写的使用上有所不同,最好只消除替代情况。例如,在 Linux 中,可以在同一目录中包含 MyClass.py 和 myclass.py。在 Windows!
中并非如此相关说明,如果您在 git 存储库中有 MyClass.py 和 myclass.py,或者甚至只是更改同一文件的大小写,git 可以当你 push/pull 在 Linux 和 Windows 对面时表现得很时髦。
而且,虽然几乎没有主题,但本着同样的精神,SQL 也有这些相同的问题,不同的标准和配置可能会或可能不会允许 table 名称上的大写。
就我个人而言,即使在文件名上阅读 TitleCasing / camelCasing 也更令人愉快,但是当您做任何可以跨平台工作的事情时,最好不要这样做。
class 名称的命名约定与包含此 class 的文件不同。这种误解可能来自 java 这样的语言,在这种语言中,每个 class.
有一个文件是很常见的在 python 中,每个模块(一个简单的 .py 文件)可以有几个 classes。此 module/file 中的 classes 应根据 class 命名约定进行调用:Class 名称通常应使用 CapWords 约定。 包含此 classes 的文件应遵循模块命名约定:模块应具有简短的全小写名称。如果提高可读性,可以在模块名称中使用下划线。
=> CamelCase 应该在文件中 camelcase.py(或者 camel_case.py 如果需要的话)