-
-
Save katyukha/4353106 to your computer and use it in GitHub Desktop.
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 AttrDict(dict): | |
""" Simple class to make dictionary able to use attribute get operation | |
to get elements it contains using syntax like: | |
>>> d = AttrDict(arg1=1, arg2='hello') | |
>>> print d.arg1 | |
1 | |
>>> print d.arg2 | |
hello | |
>>> print d['arg2'] | |
hello | |
>>> print d['arg1'] | |
1 | |
""" | |
def __getattribute__(self, name): | |
try: | |
return super(AttrDict, self).__getattribute__(name) | |
except AttributeError: | |
try: | |
return super(AttrDict, self).__getitem__(name) | |
except KeyError: | |
raise AttributeError(name) | |
def __setattr__(self, name, value): | |
self[name] = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment