Compare frozendict and sentinel with Earlier Alternatives
Core idea
Python 3.15 adds frozendict for immutable mappings and sentinel for distinct named marker objects. A frozendict preserves insertion order but equality ignores that order; hashing requires hashable keys and values. It is not a dict subclass. MappingProxyType is an older read-only view of a mapping, not a frozen snapshot. A sentinel can distinguish an omitted argument from an explicit None; unlike the older object() marker, it has a name and preserves identity through copy operations.
first = frozendict(region='west', retries=2)second = frozendict(retries=2, region='west')print(list(first))print(first == second, hash(first) == hash(second))try: first['retries'] = 3except TypeError: print('immutable')The stored order is visible during iteration; equal hashable mappings have equal hashes. The specific hash value is not asserted.
Documented expected output (target runtime not tested locally):
['region', 'retries']True TrueimmutableTarget: Python 3.15 distinguish omitted from null
from copy import copyMISSING = sentinel('MISSING')def describe(value=MISSING): if value is MISSING: return 'omitted' if value is None: return 'explicit null' return str(value)print(describe(), describe(None))print(copy(MISSING) is MISSING, sentinel('MISSING') is MISSING)Copying preserves the marker, but each constructor call creates a distinct sentinel even with the same name.
Documented expected output (target runtime not tested locally):
omitted explicit nullTrue FalseCommon mistake
Frozendict does not recursively freeze values; a contained list is still mutable and prevents hashing. Do not compare a sentinel by its spelling or truthiness. Reuse the original object with is; pickling requires the sentinel to be resolvable by module and name. Older runtimes need separate fallbacks.
Practice
On 3.15, compare a frozendict made from a dict with a MappingProxyType over that dict after the original is changed. Then test a named sentinel with None, 0 and an omitted argument. Avoid loading untrusted pickle data.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Frozendict and sentinel
Check your understanding of frozendict and sentinel.
On Python 3.15 (documented expectation; target runtime not tested locally), what is printed?
a = sentinel('MARK')b = sentinel('MARK')print(a is b)What is printed?
from types import MappingProxyTyped = {'n': 1}p = MappingProxyType(d)d['n'] = 2print(p['n'])When can frozendict be hashed?
What is the appropriate test for an omitted sentinel argument?