Created
May 23, 2018 07:14
-
-
Save rbarzic/f1b888a75fed44df0583257dc3221c50 to your computer and use it in GitHub Desktop.
Autovification in Python using a dedicated class
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 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