六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 103|回复: 0

python学习笔记

[复制链接]

升级  10%

15

主题

15

主题

15

主题

秀才

Rank: 2

积分
65
 楼主| 发表于 2013-1-19 04:06:50 | 显示全部楼层 |阅读模式
在学习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!!!!!
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表