Skip to content

Instantly share code, notes, and snippets.

@gbvanrenswoude
Created December 12, 2022 08:40
Show Gist options
  • Save gbvanrenswoude/031cd5f1eff3fe395438f14395527545 to your computer and use it in GitHub Desktop.
Save gbvanrenswoude/031cd5f1eff3fe395438f14395527545 to your computer and use it in GitHub Desktop.
FrozenDict
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