Created
October 26, 2015 21:56
-
-
Save nicwolff/c3f40e34e1652fc4bfde to your computer and use it in GitHub Desktop.
Python dict subclass that can be indexed with object.attribute syntax
This file contains hidden or 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 dotdict(dict): | |
| def __getattr__(self, attr): | |
| return self[attr] | |
| def __missing__(self, key): | |
| self[key] = dotdict() | |
| return self[key] | |
| __setattr__ = dict.__setitem__ | |
| __delattr__ = dict.__delitem__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment