Using inheritance to remove functionality violates Liskov substitution (a hypothetical dict
subclass would behave erroneously when treated as a dict
instance). And besides, I can easily just dict.clear(my_subclassed_dict_instance)
. The thing you're asking for is not, in Python terminology, a dict
and shouldn't masquerade as a subclass of one.
What you're looking for is a fresh class entirely. You want a class that contains a dict
, not one that is a dict
.
import collections.abc.Mapping
class MyAppendOnlyContainer(Mapping):
def __init__(self, **kwargs):
self._impl = kwargs
def __getitem__(self, key):
return self._impl[key]
def __setitem__(self, key, value):
self._impl[key] = value
def __iter__(self):
return iter(self._impl)
def __len__(self):
return len(self._impl)
# Plus whatever other functionality you need ...
Note that collections.abc.Mapping
requires __getitem__
, __iter__
, and __len__
(which your use case faithfully implements) and gives you get
, __contains__
, and several other useful dict
-like helper functions. What you have is not a dict
. What you have is a Mapping
with benefits.
dict()
and override all the methods that can be used to remove items.__del__
, which is used fordel mydict[x]
__delattr_
.