Created
August 26, 2017 04:58
-
-
Save prandelicious/a2d577b52aa18222bb3497c0c80947e4 to your computer and use it in GitHub Desktop.
Sub-classing dict for better readability
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 objdict(dict): | |
def __getattr__(self, name): | |
if name in self: | |
return self[name] | |
else: | |
raise AttributeError("No such attribute: " + name) | |
def __setattr__(self, name, value): | |
self[name] = value | |
def __delattr__(self, name): | |
if name in self: | |
del self[name] | |
else: | |
raise AttributeError("No such attribute: " + name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: https://goodcode.io/articles/python-dict-object/