python的类名一定要大写吗,Python标准库中类名的大写标准是什么?

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?

解决方案

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值