- 在构建MySQL健康检查器的时候,遇到以下
dict.update
语法:
def sync_error(action_name, error, **kwargs):
resp_dict = {}
resp_dict['action'] = action_name
resp_dict['is_success'] = False
error = str(error) if isinstance(error, Exception) else error
resp_dict['error'] = error
if kwargs:
resp_dict.update(kwargs)
return resp_dict
- 经过查阅对于
dict object
的update
函数的理解如下:
- 使用方法如下:
dict.update(dict2)
-
dict2
-- 添加到指定字典dict
里的字典。
- 举个栗子🌰:
In [82]: dic = {"A":"a", "B":"b"}
# print 出初始的 dict 对象, 输出结果为 {"A":"a", "B":"b"}
In [83]: print dic
{'A': 'a', 'B': 'b'}
# 如果update的键值在字典中已经存在的话,更新该对应的键值
In [84]: dic.update(A="Aa")
# 输出 {'A': 'Aa', 'B': 'b'}
In [85]: print dic
{'A': 'Aa', 'B': 'b'}
# 如果添加的键值不存在,加入键值对到字典中
In [86]: dic.update(C="C")
# 输出 {'A': 'Aa', 'C': 'C', 'B': 'b'}
In [87]: print dic
{'A': 'Aa', 'C': 'C', 'B': 'b'}
网友评论