About the sub-class inheriting immutable rules from the parent, you might wish to look into:
- Interface class contracting & inheritance. Technically you can increase code-safety by making strict rules through inherited interface class or classes*.
- Constructors
- Maybe even defining the class as a singleton could help (one class can be instantiated only once, aka to a one/single object), Python support for singleton has been getting a bit more better during last years.
- Automatic Programming / Code generation tactics & best-practises
Below is different mechanisms (I could quickly imagine, probably not comprehensive list!) that you can increase code-safety in Python (as it is not strictly typed, nor it has honestly very good mechanisms for writing bullet-proof code in such way that this type of things could be forced already by the compiler, as it has no compiler).
Although this does not make it "real constant", it still offers you protection for the object being changed.
- One additional thing if focusing on the code-safety by wrapping the dictionary/dictionaries inside this type of class, is that might find it feasible to look how to make a singleton class as well as how to use a constructor, def init() in your problem.
Depending on your code safety needs, you might still wish to:
- instantiate the whole dictionary from read-only json (that ensure the structure etc.), use json schema-definition to give it another layer of validation as well.
- you might wish to encode your class instance & its dictionary objects in Python, then provide only Read-functionality which does decoding before reading the dict. This is slight perf impact, but if your dictionary contains feasible amount of data (not "big data"), and there is no need for real-time high-perf + the amount of this type of dictionaries is rather moderate (not tens of thousands/millions, decoding it in read-function beginning should not be a problem.
*Last time I checked, Python still had no true built-in Interface-class support, its from an external library.
Through built-in lib called abc (abstract base class) you can "imitate" true interface implementation, although this as the name says is for making classes abstract, not for creating interfaces that are in my opinion better way to do ruffly the same, without creating plethora of code-locks that echo as a code-duplication on the child object level.
Also, with interface-class you can loop things by it, I think abstract classes cannot be used in such a sense.
Let'say:
IConstantSingleton (contract) --> inherit to ConstSingletonBaseObject (base/parent) --> inherit to any various children (instantiated child objects), where children can be required to fulfill the contract details through passing the responsibilities from abstraction of the base/parent), or from the virtual definition in base/parent function(s) (that Python has a little support nowadays already), that allows you to implement only overrides (less-code duplication!).
Surely, you can contract also child-classes directly, without inheriting the contract, especially if there is such virtual override need for example, additional contract for those special-snowflakes can increase the code-safety and robustness (and future development based on it)
__setitem__()
method of your dict. Note however, that this doesn't guarantee that the values of your dict will be immutable (say you have values that are lists, for example). – Feverish__delitem__()
,clear()
,pop()
,popitem()
,setdefault()
andupdate()
. – Tacitatuple
is immutable ? – Wreckful