首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > PowerDesigner >

python学习札记 - dict.update

2012-12-19 
python学习笔记 - dict.update在学习tornado template源码的时候,遇到以下dict.update语法: def generate(

python学习笔记 - dict.update
在学习tornado template源码的时候,遇到以下dict.update语法:

 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 existsdic.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 objectdic.update(C="C")# output {'A': 'Aa', 'C': 'C', 'B': 'b'}print dic


;) ! Move on!!!!!

热点排行