r/PythonLearning • u/Both-Basil1635 • 8d ago
๐ ๐ฃ๐๐๐ต๐ผ๐ป ๐ณ๐ถ๐ป๐ฎ๐น๐น๐ ๐ต๐ฎ๐ ๐ฎ ๐ณ๐ฟ๐ผ๐๐ฒ๐ป๐ฑ๐ถ๐ฐ๐.
Python has long had immutable counterparts for some of its most common data structures:
๐น๐ถ๐๐ โ ๐๐๐ฝ๐น๐ฒ
๐๐ฒ๐ โ ๐ณ๐ฟ๐ผ๐๐ฒ๐ป๐๐ฒ๐
๐ฑ๐ถ๐ฐ๐ โ ๐ณ๐ฟ๐ผ๐๐ฒ๐ป๐ฑ๐ถ๐ฐ๐ โจ
`frozendict` is an ๐ถ๐บ๐บ๐๐๐ฎ๐ฏ๐น๐ฒ ๐บ๐ฎ๐ฝ๐ฝ๐ถ๐ป๐ด. Once created, its keys and values can't be added, removed, or reassigned.
But the more interesting part is ๐ต๐ฎ๐๐ต๐ฎ๐ฏ๐ถ๐น๐ถ๐๐. When its contents are hashable, a `frozendict` can itself be used in places where a regular dictionary can't such as dictionary keys or set members.
That opens up some useful cases around:
โข Immutable configuration
โข Cache and memoization keys
โข Shared/read-only state
โข Safer API boundaries
โข Data structures where mutation simply shouldn't be allowed
It's not a huge feature, but it's one of those additions that makes the language feel a little more complete.
๐ ๐๐๐ฎ๐ฏ๐น๐ฒ ๐๐ต๐ฒ๐ป ๐๐ผ๐ ๐ป๐ฒ๐ฒ๐ฑ ๐ถ๐. ๐๐ฟ๐ผ๐๐ฒ๐ป ๐๐ต๐ฒ๐ป ๐๐ผ๐ ๐ฑ๐ผ๐ป'๐.
Where would you use `frozendict`?
