Python 标准库类的规范似乎是类名是小写的——这似乎适用于内置函数,例如 str 和 int 以及对于大多数属于必须导入的标准库模块的类,例如 datetime.date 或 datetime.datetime。
但是,某些标准库类,例如 enum.Enum 和 decimal.Decimal 是大写的。乍一看,当类的名称等于模块名称时,它们似乎是大写的,但这并不适用于所有情况(例如 datetime.datetime)。
Python 标准库中类名大写约定背后的基本原理/逻辑是什么?
请您参考如下方法:
Key Resources开发人员指南的一部分列出了 PEP 8作为风格指南。
来自 PEP 8 命名约定,强调我的。
The naming conventions of Python's library are a bit of a mess, so we'll never get this completely consistent -- nevertheless, here are the currently recommended naming standards. New modules and packages (including third party frameworks) should be written to these standards, but where an existing library has a different style, internal consistency is preferred.
A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important. ...
Some other good reasons to ignore a particular guideline:
- To be consistent with surrounding code that also breaks it (maybe for historic reasons) -- although this is also an opportunity to clean up someone else's mess (in true XP style).
- Because the code in question predates the introduction of the guideline and there is no other reason to be modifying that code.
您可能永远不会知道为什么标准库命名约定与 PEP 8 冲突,但对于新内容甚至在您自己的项目中遵循它可能是个好主意。
