Skip to content

Instantly share code, notes, and snippets.

@pointofpresence
Created April 4, 2022 13:08
Show Gist options
  • Save pointofpresence/c418e8e748f2daa15b8a25975be0ae59 to your computer and use it in GitHub Desktop.
Save pointofpresence/c418e8e748f2daa15b8a25975be0ae59 to your computer and use it in GitHub Desktop.
How to use a dot "." to access members of dictionary?
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
mydict = {'val':'it works'}
nested_dict = {'val':'nested works too'}
mydict = dotdict(mydict)
mydict.val
# 'it works'
mydict.nested = dotdict(nested_dict)
mydict.nested.val
# 'nested works too'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment