On 30/04/17 01:31, Ben Finney wrote:
Erik <pyt...@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? :-)

Yes, I did. Which part of Ned's do you think I did not read properly?

You say that there are only two methods you want to change the behaviour
of;

Yes, __init__ (so that I can store the valid keys) and __setitem__ (so that I can check the key being set).

but as you have found, those two methods are not the only ones you
need to implement, in order to get the changed behaviour.

I didn't find that. But I found that I could change exactly those two methods if I just derived from a different class that also provides another behavior that I don't need (ordered keys).

you need to implement the MutableMapping
protocol if you want a custom class

Why do I "need" to do that when I could subclass from OrderedDict instead?

If I'm not being clear:

Is there not a class that is somewhere between "dict" and "OrderedDict" that provides what I need? collections.abc.MutableMapping is not that class, as it requires me to write all sorts of methods that are irrelevant to the behavior changes I want to make (__len__ etc).

E.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to