Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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?

like image 832
Baz Avatar asked Sep 23 '13 07:09

Baz


People also ask

Which of these naming convention starts with lowercase?

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.

What is the naming convention used in Python?

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)

What is the uppercase naming style?

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.

Should Python functions be capitalized?

According to PEP8, function and variable names should be lowercase with words separated by underscores.


1 Answers

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.

like image 65
TerryA Avatar answered Sep 19 '22 08:09

TerryA