Created
November 14, 2012 06:43
-
-
Save bbrodriges/4070691 to your computer and use it in GitHub Desktop.
Dictionary with key/values as attributes
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 AttrDict(dict): | |
""" A dict that allows for object-like property access syntax. """ | |
def __getattr__(self, name): | |
try: | |
return self[name] | |
except KeyError: | |
raise AttributeError(name) | |
def __setattr__(self, key, value): | |
self.update({key:value}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment