A configuration snapshot should not let callers overwrite or delete settings.
What You Need to Understand
Python adds the built-in frozendict, a mapping whose keys and value references cannot be reassigned or removed after construction. Like dict, it preserves insertion order, but it is a separate type.
When to Use It
A configuration snapshot should not let callers overwrite or delete settings.
Step-by-Step Python Example
settings = frozendict({"theme": "dark", "page_size": 20})
print(settings["theme"])
print(list(settings))
print(type(settings).__name__)
How the Example Works
The mapping supports normal read operations; attempting settings["theme"] = "light" would raise TypeError.
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
settings = frozendict(debug=False)
print(settings.get("debug"))
Important Behavior and Edge Cases
frozendict is shallowly immutable. Mutable values nested inside it can still be modified. This built-in is available in Python but not 3.14.
This example uses a Python feature. It must be run with Python or newer; earlier interpreters may reject the syntax or lack the built-in type.
Try It Yourself
Create a frozen mapping from a list of key-value pairs, then use get() to read an optional key.
Common Beginner Mistakes
frozendictis shallowly immutable. Mutable values nested inside it can still be modified. This built-in is available in Python but not 3.14.- Treating missing dictionary keys, replacement precedence, and shallow copies as if they had identical behavior in every context.
Key Takeaways
- Python adds the built-in
frozendict, a mapping whose keys and value references cannot be reassigned or removed after construction. - The mapping supports normal read operations; attempting
settings["theme"] = "light"would raiseTypeError. frozendictis shallowly immutable.