强大的Python生成器
逐步演进
f=open('/etc/motd','r')
longest=0
while True:
lineLen=len(f.readline().strip())
if not lineLen: break
if lineLen > longest:
longest=lineLen
f.close()
return longest
问题:一直占有文件句柄未释放
f=open('/etc/motd','r')
longest=0
allLines=f.readLines()
f.close()
for line in allLines:
lineLen=len(line.strip())
if lineLen > longest:
longest=lineLen
return longest
问题:读完文件所有行再开始计算,耗费大量内存
f=open('/etc/motd','r')
allLineLens=[len(x.strip()) for x in f]
f.close()
return max(allLineLens)
问题:文件迭代器一行一行迭代f,列表解析需要文件所有行都会读到内存中
f=open('/etc/motd','r')
longest=max(len(x.strip()) for x in f)
f.close()
return longest
问题:可去掉文件打开模式,让python去处理打开的文件
return max(len(x.strip()) for x in open('etc/motd'))
这个完美了。
相关文档:
软件准备:
1.eclipse开发包,下载地址:http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/galileo/SR2/eclipse-java-galileo-SR2-win32.zip
2.pydev插件,下载地址:http://sourceforge.net/projects/pydev/files/pydev/Pydev%201.5.4/org.python.pydev.feature-1.5.4.2010011921 ......
集合类型操作符(所有的集合类型)
联合( | )
联合(union)操作和集合的OR(又称可兼析取(inclusive
disjunction))其实是等价的,两个集
合的联合是一个新集合,该集合中的每个元素都至少是其中一个集合的成员,即,属于两个集合其
中
之一的成员。联合符号有一个等价的方法,union().
Edit By Vheavens
Edit By Vhe ......
最近使用python过程中,python界面的编程工具GTK-Python,但是界面的美观性不如Qt-Creator中的Qt-Designer,无法实现设计是视图绘制,有点让人失望。
网上发现有人介绍python Eric IDE,比较好奇,安装上看看吧:
&nb ......
python的C、c++扩展
http://blog.chinaunix.net/u3/110228/showart_2148725.html
python的强大不仅表现在其功能上,而且还表现在其扩展能力上。
使用C/C++很容易编写python的模块,扩展python的功能。
同时将性能要求比较高的代码使用C/C++编写,能更好的弥补
脚本语言执行速度慢的缺陷。
1. python的C语言扩展
1.1 ......
Python的内存泄漏及gc模块的使用
-- 6.11日错误修正版
Horin|贺勤
Email: horin153@msn.com
......