12

Is there any significance to the capitalization practices for classes in the collections module in Python? In particular, I find it puzzling that OrderedDict uses CamelCase while defaultdict is all lowercase. My assumption would be they would all use CamelCase since they are all classes.

enter image description here

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
willk
  • 3,727
  • 2
  • 27
  • 44
  • I think it's to do with primary C implementation vs python implementation (for CPython) – Chris_Rands Jul 23 '19 at 14:13
  • As in Python implementations (`OrderedDict`, `Counter`) are CamelCase and primary C implementations are all lowercase? – willk Jul 23 '19 at 14:17
  • 1
    Yes, but this isn't true (now), it might be that it used to be true (i.e. legacy)- i'll try and check – Chris_Rands Jul 23 '19 at 14:19
  • 2
    There are some hysterical raisins here - back from the 1.x days when builtin types and user defined types were totally different animals and builtin types names (`list', 'dict` etc) were actually factory functions, not classes. As to why some types are spelled in all_lower and some in CamelCase, I'm afraid Chris_Rands is mostly right - except for when some type has been initially implemented in pure python then re-implemented in C. – bruno desthuilliers Jul 23 '19 at 14:28
  • 1
    Straight from PEP8: "The naming conventions of Python's library are a bit of a mess, so we'll never get this completely consistent". – MisterMiyagi Jul 23 '19 at 14:32

1 Answers1

5

The naming is historical (for CPython). Originally, CamelCase classes (like OrderedDict) were pure python implemented and other classes (like defaultdict) were C-implemented. However, now the names are just legacy (mostly), since C-implementation has often been added (e.g. here for OrderedDict- you can see now that the python implementation is only a fallback).

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
  • Thanks for the answer. I like knowing there's some reasoning, even if it no longer is consistent. – willk Jul 23 '19 at 14:35