Skip to content

Instantly share code, notes, and snippets.

@rbarzic
Created May 23, 2018 07:14
Show Gist options
  • Save rbarzic/f1b888a75fed44df0583257dc3221c50 to your computer and use it in GitHub Desktop.
Save rbarzic/f1b888a75fed44df0583257dc3221c50 to your computer and use it in GitHub Desktop.
Autovification in Python using a dedicated class
class AutoVivification(dict):
"""Implementation of perl's autovivification feature."""
def __getitem__(self, item):
try:
return dict.__getitem__(self, item)
except KeyError:
value = self[item] = type(self)()
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment