Python标准库中容器的命名约定

2024-04-25 11:43:01 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑Python标准库中不同类型的containers使用的命名约定:

为什么有些方法遵循camel case,而其他方法如dequedefaultdict则不遵循?这些方法在解释这种差异方面有什么不同

如果是因为约定在某个时候发生了变化,那么为什么模块(例如)不向alias提供旧名称的驼峰大小写名称呢


Tags: 模块方法名称类型标准alias差异命名
1条回答
网友
1楼 · 发布于 2024-04-25 11:43:01

通常在python中,类名遵循“pascal”case约定,方法/函数遵循“snake”case约定。 但以下是来自https://www.python.org/dev/peps/pep-0008/的官方参考:

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

Class names

Class names should normally use the CapWords convention.

The naming convention for functions may be used instead in cases where the interface is documented and used primarily as a callable.

Note that there is a separate convention for builtin names: most builtin names are single words (or two words run together), with the CapWords convention used only for exception names and builtin constants.

相关问题 更多 >