Skip to content

Instantly share code, notes, and snippets.

@hvuhsg
Last active October 27, 2021 15:16
Show Gist options
  • Save hvuhsg/e35908461444868b26839300cb9ab976 to your computer and use it in GitHub Desktop.
Save hvuhsg/e35908461444868b26839300cb9ab976 to your computer and use it in GitHub Desktop.
Use dot to access dict values
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