A cache uses a set of configuration options as a lookup key.
What You Need to Understand
A frozendict is hashable only when all of its keys and values are hashable. Immutable outer structure does not mean nested values are immutable.
When to Use It
A cache uses a set of configuration options as a lookup key.
Step-by-Step Python Example
safe_settings = frozendict({"mode": "fast", "retry": 3})
print(hash(safe_settings) == hash(frozendict(retry=3, mode="fast")))
unsafe_settings = frozendict({"tags": ["a"]})
try:
hash(unsafe_settings)
except TypeError:
print("Nested list prevents hashing")
How the Example Works
Equal frozen mappings have equal hashes, regardless of insertion order. A nested list is unhashable and prevents hashing the outer mapping.
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
configuration = frozendict(flags=("a", "b"))
print(isinstance(hash(configuration), int))
Important Behavior and Edge Cases
Do not mistake hashability for encryption or security. Hash values are process-level lookup aids and must not be stored as stable cryptographic identifiers.
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
Replace the list with a tuple and check that hashing succeeds.
Common Beginner Mistakes
- Do not mistake hashability for encryption or security. Hash values are process-level lookup aids and must not be stored as stable cryptographic identifiers.
- Treating missing dictionary keys, replacement precedence, and shallow copies as if they had identical behavior in every context.
Key Takeaways
- A
frozendictis hashable only when all of its keys and values are hashable. - Equal frozen mappings have equal hashes, regardless of insertion order.
- Do not mistake hashability for encryption or security.