Categories
Python Answers

How to use a dot “.” to access members of dictionary with Python?

Spread the love

Sometimes, we want to use a dot "." to access members of dictionary with Python.

In this article, we’ll look at how to use a dot "." to access members of dictionary with Python.

How to use a dot "." to access members of dictionary with Python?

To use a dot "." to access members of dictionary with Python, we can use the dotmap library.

To install it, we run

pip install dotmap

Then we can use it by writing

from dotmap import DotMap

m = DotMap()
m.hello = 'world'
m.hello
m.hello += '!'
m.val = 5
m.val2 = 'Sam'

to create a DotMap object, which is a subclass of dict.

Then we assign values to properties of m.

And then we can use m.hello or m['hello'] to access properties of m.

We can convert m back to a regular dict with

d = m.toDict()

And we can convert a dict to a DotMap object with

m = DotMap(d) 

where d is a dict.

Conclusion

To use a dot "." to access members of dictionary with Python, we can use the dotmap library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *