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

Answer by Emil Stenström for How to use a dot "." to access members of dictionary?

$
0
0

One simple way to get dot access (but not array access), is to use a plain object in Python. Like this:

class YourObject:    def __init__(self, *args, **kwargs):        for k, v in kwargs.items():            setattr(self, k, v)

...and use it like this:

>>> obj = YourObject(key="value")>>> print(obj.key)"value"

... to convert it to a dict:

>>> print(obj.__dict__){"key": "value"}

Viewing all articles
Browse latest Browse all 40

Trending Articles



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