gpt4 book ai didi

python - 集合中的命名约定 : why are some lowercase and others CapWords?

转载 作者:太空狗 更新时间:2023-10-29 17:19:46 24 4
gpt4 key购买 nike

为什么要混合使用小写字母和大驼峰字母?

namedtuple
deque
Counter
OrderedDict
defaultdict

为什么使用 collections 而不是 Collections

我有时会这样做,例如:

from collections import default_dict

错误。我可以使用什么经验法则来避免将来出现此类错误?

最佳答案

集合模块遵循 PEP 8 Style Guide :

Modules should have short, all-lowercase names.

这就是为什么它是collections


Almost without exception, class names use the CapWords convention.

这就是为什么它是CounterOrderedDict,因为它们都是类:

>>> collections.Counter
<class 'collections.Counter'>
>>> collections.OrderedDict
<class 'collections.OrderedDict'>

namedtuple 是一个函数,所以它不遵循上面提到的风格指南。 dequedefaultdict 是类型,所以它们也不是:

>>> collections.deque
<type 'collections.deque'>
>>> collections.namedtuple
<function namedtuple at 0x10070f140>
>>> collections.defaultdict
<type 'collections.defaultdict'>

注意:在 Python 3.5 中,defaultdict 和 deque 现在也是类:

>>> import collections
>>> collections.Counter
<class 'collections.Counter'>
>>> collections.OrderedDict
<class 'collections.OrderedDict'>
>>> collections.defaultdict
<class 'collections.defaultdict'>
>>> collections.deque
<class 'collections.deque'>

我假设他们将 defaultdictdeque 保留为小写以便向后兼容。我无法想象他们会为了风格指南而做出如此剧烈的名称更改。

关于python - 集合中的命名约定 : why are some lowercase and others CapWords?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18953681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com