Created
October 31, 2014 09:55
-
-
Save darkf/d532edb0ac4307c8725c to your computer and use it in GitHub Desktop.
JS-style lookups for Python dicts
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 __getattribute__(self, key): | |
if key in self: | |
return self[key] | |
return dict.__getattribute__(self, key) | |
d = dotdict(a=1, b=2) | |
print d.a, d.b | |
print d.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment