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

Ben Finney ben+python at benfinney.id.au
Sat Apr 29 20:31:23 EDT 2017


Erik <python at lucidity.plus.com> writes:

> On 29/04/17 23:40, Ned Batchelder wrote:
> > 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__.
>
> Or, I could derive from collections.OrderedDict and just implement the
> two methods that I actually want to change the behavior of (did you
> read the rest of my post?) ;)

Did you read Ned's? :-)

You say that there are only two methods you want to change the behaviour
of; but as you have found, those two methods are not the only ones you
need to implement, in order to get the changed behaviour.

> That's one of the points I'm trying to make - why is it harder than it
> needs to be to do something this simple?

The “needs” have already been expressed, by yourself and Ned: the type
needs to provide fast performance. One cost of that, with the current
implementation, is that you need to implement the MutableMapping
protocol if you want a custom class.

-- 
 \        “Perchance you who pronounce my sentence are in greater fear |
  `\   than I who receive it.” —Giordano Bruno, burned at the stake by |
_o__)  the Catholic church for the heresy of heliocentrism, 1600-02-16 |
Ben Finney




More information about the Python-list mailing list