EasyDict is a subclass of Python's built-in dictionary that allows accessing dictionary keys as attributes like JavaScript object.
pip install easydict
from easydict import EasyDict as edict
my_dict = edict({
"name": "John",
"age": 30,
"location": {
"city": "New York",
"state": "NY"
}
})
print(my_dict['name']) # Output: John
print(my_dict.name) # Output: John
print(my_dict.location.city) # Output: New York
type(my_dict) # easydict.EasyDict
Conversion Back to Dictionary
normal_dict = dict(my_dict)
Normal dictionary::
data = {
"name": "John",
"age": 30,
"location": {
"city": "New York",
"state": "NY"
}
}
data["location"]["city"] # New York
data.location.city # 'dict' object has no attribute 'location'