Let's say you used a frozendict
implementation like this one:
class frozendict(collections.Mapping):
"""
An immutable wrapper around dictionaries that implements the complete :py:class:`collections.Mapping`
interface. It can be used as a drop-in replacement for dictionaries where immutability is desired.
"""
dict_cls = dict
def __init__(self, *args, **kwargs):
self._dict = self.dict_cls(*args, **kwargs)
self._hash = None
def __getitem__(self, key):
return self._dict[key]
def __contains__(self, key):
return key in self._dict
def copy(self, **add_or_replace):
return self.__class__(self, **add_or_replace)
def __iter__(self):
return iter(self._dict)
def __len__(self):
return len(self._dict)
def __repr__(self):
return '<%s %r>' % (self.__class__.__name__, self._dict)
def __hash__(self):
if self._hash is None:
h = 0
for key, value in self._dict.items():
h ^= hash((key, value))
self._hash = h
return self._hash
If you wanted to mutate it, you could just reach in and mutate self._dict
:
d = frozendict({'a': 1, 'b': 2})
d['a'] = 3 # This fails
mutable_dict = d._dict
mutable_dict['a'] = 3 # This works
print(d['a'])
It's a little yucky reaching into a class's protected members, but I'd say that's okay because what you're trying to do is a little yucky. If you want a mutable dictionary (just a dict
) then use one. If you never want to mutate it, use a frozendict
implementation like the one above. A hyrid of mutable and immutable makes no sense. All frozendict
does is it doesn't implement the mutation dunder methods (__setitem__
, __delitem__
, etc.). Under the hood a frozendict
is represented by a regular, mutable dict
.
The above approach is superior in my view to the one you linked. Composability (having frozendict
have a _dict
property) is much easier to reason about than inheritance (subclassing dict
) in many cases.