Python标准库中类名的大写标准是什么? [英] What is the capitalization standard for class names in the Python Standard Library?

查看:319
本文介绍了Python标准库中类名的大写标准是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python标准库类的规范似乎是类名是小写的-对于诸如strint之类的内置组件以及作为标准库模块一部分的大多数类,这似乎适用必须导入的内容,例如datetime.datedatetime.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.Enumdecimal.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?

推荐答案

关键资源<开发人员指南的/a>部分列出了 PEP 8 作为样式指南.

摘自PEP 8命名约定,重点是我的.

From PEP 8 Naming Conventions, emphasis mine.

Python库的命名约定有点混乱,因此 我们将永远无法获得完全一致的结果-不过,这是 当前推荐的命名标准.新模块和软件包 (包括第三方框架)写给这些 标准,但是如果现有图书馆的风格有所不同, 首选内部一致性.

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:

  • 与也会破坏它的周围代码保持一致(也许是出于历史原因)-尽管这也是清理的机会 弄乱别人的烂摊子(真正的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标准库中类名的大写标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆