Created
January 5, 2019 16:55
-
-
Save praveenc/9e058346e803b18c3a9122b9ff73b9c9 to your computer and use it in GitHub Desktop.
Python - Access dictionary with dot notation
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
| #!/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