What is the capitalization standard for class names in the Python Standard Library?

早过忘川 提交于 2019-12-13 17:08:48

问题


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.

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

What's the rationale/logic behind the capitalization conventions for class names in the Python Standard Library?


回答1:


The Key Resources section of the Developers Guide lists PEP 8 as the style guide.


From PEP 8 Naming Conventions, emphasis mine.

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.


Also from PEP 8

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.

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.




回答2:


Pep 8 is considered to be the standard style guide by many Python devs. This recommends to name classes using CamelCase/CapWords.

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.

Check this link for PEP8 naming conventions and standards.

datetime is a part of standard library,

Python’s standard library is very extensive, offering a wide range of facilities as indicated by the long table of contents listed below. The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming.

In some cases, like sklearn, nltk, django, the package names are all lowercase. This link will take you there.

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

I hope this covers all the questions.



来源:https://stackoverflow.com/questions/41413442/what-is-the-capitalization-standard-for-class-names-in-the-python-standard-libra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!