Rodrigo Girão Serrão: frozendict built-in

Wait 5 sec.

Learn how to use the new frozendict built-in to create and manipulate immutable dictionaries.Python 3.15 introduced a new built-in data type: the frozendict.A frozendict is an immutable dictionary that can be created with the built-in of the same name:version_info = frozendict( {"major": 3, "minor": 15, "patch": 0})The dictionary literal syntax using the curly braces {} creates a regular dictionary, so you must always use the built-in frozendict to create a new frozen dictionary.In this article you'll learn about the characteristics of this immutable dictionary like its hashability, along with how to use this in conjunction with dict in typing contexts.15 days of Python 3.15To celebrate the release of Python 3.15, over the next 15 business days I'll be writing about a new 3.15 feature every day.Explained clearly and with examples so you don't have to sift through the changelog.Subscribe to receive this free email series: Enroll for free 🐍🚀 If you want, you can also check the schedule of the upcoming emails.Creation and usageYou create immutable dictionaries with the built-in frozendict and you can access key/value pairs with the regular dictionary syntax:version_info = frozendict({"major": 3, "minor": 15, "patch": 0})print(version_info["major"]) # 3You can also iterate through a frozendict's keys, values, or items:for name, number in version_info.items(): print(name, number)"""major 3minor 15patch 0"""If you don't have to support Python 3.14 or earlier, you should use frozendict whenever you're creating or returning a dictionary that isn't supposed to be explicitly and clearly used as mutable.Immutability of a frozendictThe main premise of frozendict is that it's immutable:version_info = frozendict({"major":...