在学习tornado template源码的时候,遇到以下dict.update语法:
对于dict object的update函数做下代码场景和学习笔记:
;) ! Move on!!!!!
def generate(self, **kwargs):
"""Generate this template with the given arguments."""
namespace = {
"escape": escape.xhtml_escape,
"url_escape": escape.url_escape,
"json_encode": escape.json_encode,
"squeeze": escape.squeeze,
"datetime": datetime,
}
namespace.update(kwargs)
对于dict object的update函数做下代码场景和学习笔记:
dic = {"A":"a", "B":"b"}
# print the original dict object, output {"A":"a", "B":"b"}
print dic
# update the given key to invoke the another value if the given key exists
dic.update(A="Aa")
# output {'A': 'Aa', 'B': 'b'}
print dic
# if the the given key is not existed, add this key/value pair in the target dict object
dic.update(C="C")
# output {'A': 'Aa', 'C': 'C', 'B': 'b'}
print dic
;) ! Move on!!!!!
本文通过一个具体的示例详细介绍了Python中字典(dict)的update()方法的使用方式,包括如何更新已存在的键值对以及如何添加新的键值对。
2万+

被折叠的 条评论
为什么被折叠?



