Created
April 4, 2022 13:08
-
-
Save pointofpresence/c418e8e748f2daa15b8a25975be0ae59 to your computer and use it in GitHub Desktop.
How to use a dot "." to access members of dictionary?
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 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