Inconsistency between dict() and collections.OrderedDict() methods.

Ned Batchelder ned at nedbatchelder.com
Sat Apr 29 18:40:29 EDT 2017


On Saturday, April 29, 2017 at 4:20:06 PM UTC-4, Erik wrote: 
> It seems a little onerous that I have to put the key checks in several 
> places and implement each of those APIs manually again (and keep on top 
> of that if dict() grows some new methods that involve setting items). Is 
> there a compelling reason why the dict module doesn't call a custom 
> __setitem__ each time an item needs to be set? Performance is 
> undoubtably a concern with something a fundamental as dicts...

Performance is the reason.  For creating your own class that acts like
a dict, you should derive from collections.abc.MutableMapping, which
only requires implementing __getitem__, __setitem__, __delitem__,
__iter__, and __len__.

--Ned.


More information about the Python-list mailing list