Created
December 12, 2022 08:40
-
-
Save gbvanrenswoude/031cd5f1eff3fe395438f14395527545 to your computer and use it in GitHub Desktop.
FrozenDict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FrozenDict(dict): | |
def __setitem__(self, key, value): | |
raise TypeError("This dictionary is frozen. You cannot add or modify items.") | |
def __hash__(self): | |
items = tuple(self.items()) | |
return hash(items) | |
my_dict = {"a": 1, "b": 2, "c": 3} | |
my_frozen_dict = FrozenDict(my_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment