Naming convention in Collections: why are some lowercase and others CapWords?

后端 未结 1 1710
星月不相逢
星月不相逢 2021-01-04 03:46

Why the mixture of lowercase and UpperCamelCase?

namedtuple
deque   
Counter 
OrderedDict
defaultdict

Why collections instead

相关标签:
1条回答
  • 2021-01-04 04:42

    The collections module follows the PEP 8 Style Guide:

    Modules should have short, all-lowercase names.

    This is why it's collections


    Almost without exception, class names use the CapWords convention.

    This is why it's Counter and OrderedDict, because they are both classes:

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

    namedtuple is a function, so it does not follow the style guide mentioned above. deque and defaultdicts are types, so they also do not:

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

    Note: With Python 3.5, defaultdict and deque are now classes too:

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

    I assume they kept defaultdict and deque lowercase for backwards compatibility. I would not imagine they'd make such a drastic name change for the sake of a style guide.

    0 讨论(0)
提交回复
热议问题