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

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

$
0
0

This also works with nested dicts and makes sure that dicts which are appended later behave the same:

class DotDict(dict):    def __init__(self, *args, **kwargs):        super().__init__(*args, **kwargs)        # Recursively turn nested dicts into DotDicts        for key, value in self.items():            if type(value) is dict:                self[key] = DotDict(value)    def __setitem__(self, key, item):        if type(item) is dict:            item = DotDict(item)        super().__setitem__(key, item)    __setattr__ = __setitem__    __getattr__ = dict.__getitem__

Viewing all articles
Browse latest Browse all 40

Trending Articles



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