Naming convention in collections

Why is CamelCase used in some places while lowercase used in other places?

lowercase

from collections import defaultdict, namedtuple

CamelCase

from collections import OrderedDict, UserDict

Why is camel case used in some places while lower case used in other
places?

lowercase

from collections import defaultdict, namedtuple

Historic use. But also their close relatives (dict, tuple) also have
lower case names.

camel case

from collections import OrderedDict, UserDict

Modern use.

Mostly builtin basic types (object, int, str, float, list) have
lowercase names.

The modern convention is that class names are conventionally CamelCase
with leading upper case letters, typical variables and parameters and
function names are snake_case and “constants” (as far as Python does
them) are UPPER_CASE.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Naming conventions are a little inconsistent due to history. And also Just Because.

Today, we have conventions like:

  • functions should be in snake_case;

  • constants should be UPPERCASE;

  • classes should be in CamelCase.

But many of the names in Python started long before we had those conventions, and for backwards-compatibility have never changed.

Built-ins like dict, int, float etc started off as pure functions, not classes. When they because classes, we kept the names for backwards compatibility.

Things like defaultdict just copied the convention of dict.

namedtuple is a factory function that returns a class, it is not a class itself.

Naming conventions are just conventions, not laws of nature. In my opinion, the only one that annoys me is object, which should be called Object so that it is easier to distinguish between generic objects, and objects which are instances of Object.

1 Like