摘要: 1,当函数没有显式地返回一个值时,例如没有执行到return object语句就结束了,他就返回None。2 finally会自动的重新引发异常。3.with语句目标的应用场景:保证共享资源的唯一分配,并在任务结束时释放它。比如文件(数据、日志、数据库等等)、线程资源、简单同步、数据库连接等等。类似于try...except但简化代码。with context_expr [as var]: with_suite with语句仅能工作于支持上下文管理协议(context management protocol)的对象,即只有内建了‘上下文管理’的对象才可以和with一起工作。 支持wit... 阅读全文
posted @ 2013-05-25 16:36 TianMG 阅读(169) 评论(0) 推荐(0) 编辑
摘要: 1 非容器类型(比如数字、字符串和其他“原子”类型的对象,像代码、类型和xrange对象等)没有拷贝一说,浅拷贝是用完全切片操作来完成的。2 如果元组变量只包含原子类型对象,对它的深拷贝将不会进行。即便是 import copy 执行了copy.deepcopy也只能得到一个浅拷贝。 阅读全文
posted @ 2013-05-25 11:28 TianMG 阅读(163) 评论(0) 推荐(0) 编辑
摘要: enumerate的使用:输出序列的索引>>> lst1=['1','2','3','4']>>> lst2=['a','b','c','d']>>> for i, l1 in enumerate(lst1): print i,l1 0 11 22 33 4zip的使用:使两个序列结合输出:>>> for l1 ,l2 in zip(lst1,lst2): print l1,"=&quo 阅读全文
posted @ 2013-05-25 11:24 TianMG 阅读(148) 评论(0) 推荐(0) 编辑
摘要: 1,变编译时字符串连接,第一种是正常(‘\’),第二种是不成用的。s1="hello world \ !!!" print 's1: ',s1s2="hello world" "!" "!!! " ' I love you' print 's2: ',s2 s1: hello world !!!s2: hello world!!!! I love you2 字符串模板1 from string import Template2 s=Template('I am 阅读全文
posted @ 2013-05-25 10:30 TianMG 阅读(211) 评论(0) 推荐(0) 编辑
摘要: 废话不多说 直接上代码:s=iter('12345')print 's.next(): ', s.next()print 'type(s.next()):' , type(s.next())print 's.next(): ', s.next()print 'type(s):' , type(s)dic ={ 'a':'c','b':'c'}d=iter(dic)print 'dic= ',dicprint 'type(d): 阅读全文
posted @ 2013-05-25 10:25 TianMG 阅读(527) 评论(0) 推荐(0) 编辑