安装:

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'})

原文地址:https://blog.csdn.net/weixin_43135178/article/details/126852828

相关文章:

  • 2022-12-23
  • 2021-07-11
  • 2021-09-09
  • 2021-09-20
  • 2022-12-23
猜你喜欢
  • 2021-12-16
  • 2021-09-10
  • 2021-11-27
  • 2021-12-12
  • 2022-12-23
  • 2021-12-09
  • 2021-11-23
相关资源
相似解决方案