Python munch包怎么使用

2023-04-24,

这篇文章主要介绍了Python munch包怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python 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'})

关于“Python munch包怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Python munch包怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注本站行业资讯频道。

《Python munch包怎么使用.doc》

下载本文的Word格式文档,以方便收藏与打印。