9

Consider the naming convention used for different types of containers in the Python standard library:

Why do some methods follow camel case, but others like deque and defaultdict don't? How are these methods different from each other in a way that would explain this difference?

If it's because at some point the convention changed, why wouldn't the module e.g. provide alias them with camel case names to old names as well?

Josh
  • 9,149
  • 16
  • 48
  • 81
  • 2
    Duplicate of [Naming convention in Collections: why are some lowercase and others CapWords?](https://stackoverflow.com/q/18953681/8601760), [Why is OrderedDict named in camel case while defaultdict is lower case?](https://stackoverflow.com/q/25597121/8601760) and [Why is collections.Counter uppercase and collections.defaultdict is not?](https://stackoverflow.com/q/33636940/8601760) – aaron Jul 23 '20 at 17:18

1 Answers1

10

Usually in python, class names follow the "pascal" case convention, methods / functions follow the "snake" case convention. But here is a official reference from https://www.python.org/dev/peps/pep-0008/:

Package and Module Names

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket).

Class names

Class names should normally use the CapWords convention.

The naming convention for functions may be used instead in cases where the interface is documented and used primarily as a callable.

Note that there is a separate convention for builtin names: most builtin names are single words (or two words run together), with the CapWords convention used only for exception names and builtin constants.

omerfarukdogan
  • 799
  • 8
  • 25
Michael Sidoroff
  • 1,331
  • 4
  • 13
  • 25
  • Thanks but `defaultdict` does not seem to follow either convention. Also `defaultdict` is of type `type`. How do you explain that with your answer? – Josh Jul 19 '20 at 21:35
  • 1
    What about `deque`? `print(deque().__class__)` -> `` – Jan Stránský Jul 19 '20 at 21:36
  • I've just updated the answer - the reason is that deque() is callable, so by convention uses all small letters. Classes that are meant to be instantiated use the camel script convention. – Michael Sidoroff Jul 19 '20 at 21:46
  • Thanks @MichaelSidoroff, maybe I am still missing this, but where does `defaultdict` fall based on what you posted above? – Josh Jul 19 '20 at 21:49
  • 1
    Also, both `deque` and `Counter` are of type `type` and callables. – Josh Jul 19 '20 at 21:53
  • Sure, remember that it's only a convention, and not a rule, but the best intuition for why deque is small case, and Counter is 'camel' case is - ask yourself if this object is operative? (i.e. if you use it as a function, or just to store data) Also in python everything is an object, so in my opinion this is the difference and the guidance for naming convention. – Michael Sidoroff Jul 19 '20 at 21:58
  • 1
    thanks, when you say: _"ask yourself if this object is operative"_ What do you mean by `operative`? – Josh Jul 19 '20 at 21:59
  • by operative I mean - if you use it as a function (e.g. deque() to be used for popping and pushing objects), or just to store data (e.g. UserList, that has only the 'data' attribute). Counter, in my opinion is functional, but I think that is upper case because it mainly stores data. – Michael Sidoroff Jul 19 '20 at 22:09
  • @Josh - If my answer does answer your question, I'd apricate if you could consider accepting it as an answer. Cheers. – Michael Sidoroff Nov 03 '20 at 07:16