Skip to content

Instantly share code, notes, and snippets.

@praveenc
Created January 5, 2019 16:55
Show Gist options
  • Save praveenc/9e058346e803b18c3a9122b9ff73b9c9 to your computer and use it in GitHub Desktop.
Save praveenc/9e058346e803b18c3a9122b9ff73b9c9 to your computer and use it in GitHub Desktop.
Python - Access dictionary with dot notation
#!/usr/bin/env python3
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'
# https://stackoverflow.com/questions/8930915/append-dictionary-to-a-dictionary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment