问题描述
Python标准库类的规范似乎是类名是小写的-对于诸如str
和int
之类的内置组件以及作为标准库模块一部分的大多数类,这似乎适用必须导入的内容,例如datetime.date
或datetime.datetime
.
The norm for Python standard library classes seems to be that class names are lowercase - this appears to hold true for built-ins such as str
and int
as well as for most classes that are part of standard library modules that must be imported such as datetime.date
or datetime.datetime
.
但是,某些标准库类(例如enum.Enum
和decimal.Decimal
)被大写.乍一看,类的名称等于模块名称时,似乎将其大写,但并非在所有情况下都适用(例如datetime.datetime
).
But, certain standard library classes such as enum.Enum
and decimal.Decimal
are capitalized. At first glance, it might seem that classes are capitalized when their name is equal to the module name, but that does not hold true in all cases (such as datetime.datetime
).
Python标准库中类名的大写约定背后的原理/逻辑是什么?
What's the rationale/logic behind the capitalization conventions for class names in the Python Standard Library?
推荐答案
摘自PEP 8命名约定,重点是我的.
From PEP 8 Naming Conventions, emphasis mine.
其他一些忽略特定准则的充分理由:
Some other good reasons to ignore a particular guideline:
- 与也会破坏它的周围代码保持一致(也许是出于历史原因)-尽管这也是清理的机会 弄乱别人的烂摊子(真正的XP风格).
- 因为有问题的代码早于准则的引入,因此没有其他理由修改该代码.
- 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冲突,但是在新内容甚至您自己的项目中遵循它可能是一个好主意.
You probably will never know why Standard Library naming conventions conflict with PEP 8 but it is probably a good idea to follow it for new stuff or even in your own projects.
这篇关于Python标准库中类名的大写标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!