Quantcast
Channel: How to use a dot "." to access members of dictionary? - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Answer by derek73 for How to use a dot "." to access members of dictionary?

$
0
0

I've always kept this around in a util file. You can use it as a mixin on your own classes too.

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'

Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>