Python munch包 /Munch() 的使用

 安装:

pip install munch

定义字典的三种方式: 

from munch import Munch


# 字典的定义方式1:
dict_1 = {'Age':8, 'School':'RUNOOB'}
print(dict_1)

# 字典的定义方式2:
dict_2 = dict(Age = 8, School='RUNOOB')
print(dict_2)

# 字典的定义方式3:
dict_3 = Munch()
dict_3.Age = 15
dict_3.School = 'RUNOOB'
print(dict_3)

得到结果: 

{'Age': 8, 'School': 'RUNOOB'}
{'Age': 8, 'School': 'RUNOOB'}
Munch({'Age': 15, 'School': 'RUNOOB'})

使用Munch()实现增删改

#增删改
# 增
dict_3.Weight='80kg'
print(dict_3)

# 删
del dict_3.Age
print(dict_3)

#改
dict_3.School="西安"
print(dict_3)

得到结果: 

Munch({'Age': 15, 'School': 'RUNOOB', 'Weight': '80kg'})
Munch({'School': 'RUNOOB', 'Weight': '80kg'})
Munch({'School': '西安', 'Weight': '80kg'})

猜你喜欢

转载自blog.csdn.net/weixin_43135178/article/details/126852828