Why the mixture of lowercase and UpperCamelCase?
namedtuple
deque
Counter
OrderedDict
defaultdict
Why collections
instead of Collections
?
I sometimes do this for example:
from collections import default_dict
by mistake. What rule of thumb can I use to avoid such mistakes in the future?
What is lowerCamelCase? A part of CamelCase, lowerCamelCase is a naming convention in which a name contains multiple words that are joined together as a single word. In terms of capitalization, the first word is always all lowercase letters, including the first letter.
A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)
UpperCamelCase (part of CamelCase) is a naming convention in which a name is formed of multiple words that are joined together as a single word with the first letter of each of the multiple words capitalized within the new word that forms the name.
According to PEP8, function and variable names should be lowercase with words separated by underscores.
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 defaultdict
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With