Last active
October 27, 2021 15:16
-
-
Save hvuhsg/e35908461444868b26839300cb9ab976 to your computer and use it in GitHub Desktop.
Use dot to access dict values
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 DotedDict(dict): | |
def __getitem__(self, item): | |
value = super().__getitem__(item) | |
if isinstance(value, dict): | |
value = DotedDict(value) | |
return value | |
def __getattr__(self, item): | |
return self[item] | |
dict_ = DotedDict({"a": {'b': 5}, 'e': {'34': 7}}) | |
print(dict_.a.b) # -> 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment