How to correctly implement the mapping protocol in Python?

后端 未结 1 1306
长发绾君心
长发绾君心 2020-12-06 05:49

I\'m using python-spidermonkey, which internally uses PyMapping_Check to identify if the object being used as a global (in rt.new_context(global)) implements the mapping pro

相关标签:
1条回答
  • 2020-12-06 06:20

    The collections.abc module defines the interfaces for things like Mapping, Sequence, and so on.

    By inheriting from the abstract base classes in that module, you get default implementations of some of the methods. So to be considered a Mapping, your class definition should look something like this:

    class MyMapping(collections.abc.Mapping):
        def __getitem__(self, item)
        def __iter__(self)
        def __len__(self)
    

    Inheriting from Mapping will get you 'free' implementations of most of dict's useful methods:

    • __contains__
    • keys
    • items
    • values
    • get
    • __eq__
    • __ne__

    If these default method implementations are inefficient with your custom data structure, you can always override them with your own versions.


    To be considered a MutableMapping, your class's interface should look like this:

    class MyMutableMapping(collections.abc.MutableMapping):
        def __getitem__(self, key)
        def __setitem__(self, key, item)
        def __delitem__(self, key)
        def __iter__(self)
        def __len__(self)
    

    Inheriting from MutableMapping gets you 'free' definitions of all of Mapping's methods, plus:

    • pop
    • popitem
    • clear
    • update
    • setdefault

    If you're 'rolling your own' from scratch and don't want to use an abstract base class, you should probably try to define all of the above methods, if you want your class to be strictly Liskov-substitutable for dict.

    0 讨论(0)
提交回复
热议问题