I would like to create a dictionary-like object as recommended here. Here is my example:
import collections
class DictLike(collections.MutableMapping):
def __init__(self, *args, **kwargs):
self.store = dict()
self.update(dict(*args, **kwargs))
def __getitem__(self, key):
return self.store[key]
def __setitem__(self, key, value):
self.store[key] = value
def __delitem__(self, key):
del self.store[key]
def __iter__(self):
return iter(self.store)
def __len__(self):
return len(self.store)
print(isinstance(dict(), collections.MutableMapping))
# >> True
print(isinstance(DictLike(), dict))
# >> False
For some reason, isinstance
of the instance of my class DictLike
is False
. Why is this so? I expected that inheriting and implementing MutableMapping
would be pretty much the same as normal dictionary.
I believe it is possible to override __instancecheck__
method as described here, but I don't know if it is a good idea and would be pythonic way?