Source code for

# This file is part of the pyMOR project (
# Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (

# The following implementation is based on

[docs]class FrozenDict(dict): """An immutable dictionary.""" @property def _blocked_attribute(self): raise AttributeError(f'A {type(self).__name__} cannot be modified.') __delitem__ = __setitem__ = clear = _blocked_attribute pop = popitem = setdefault = update = _blocked_attribute
[docs] def __new__(cls, *args, **kwargs): new = dict.__new__(cls) dict.__init__(new, *args, **kwargs) new._post_init() return new
def __init__(self, *args, **kwargs): # ensure that dict cannot be modified by calling __init__ pass def _post_init(self): pass
[docs] def __repr__(self): return f'FrozenDict({dict.__repr__(self)})'
[docs] def __reduce__(self): return (type(self), (dict(self),))
[docs]class SortedFrozenDict(FrozenDict): """A sorted immutable dictionary."""
[docs] def __new__(cls, *args, **kwargs): new = dict.__new__(cls) # the following only works on Python >= 3.7 or CPython >= 3.6 dict.__init__(new, sorted(dict(*args, **kwargs).items())) new._post_init() return new
[docs] def __repr__(self): return f'SortedFrozenDict({dict.__repr__(self)})'