易截截图软件、单文件、免安装、纯绿色、仅160KB

强大的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'))
这个完美了。


相关文档:

Python Tips (1)

最近在从头开始学习Python, 希望用blog顺便记录下来一些小的技巧。
今天记录第一个: variable _
在python的交互session中,也就是不带文件名直接输入"Python”之后python所创建的session,
变量"_"会保存上一次计算的结果。例如:

这个变量对经常把python当计算器用的同学可能有用。
参考:sys.displayhook( ......

python 的函数Decorators

Decorators是python中比较难以理解的东西,当然如果你接触过java的annotation,会发现这个Decorators在语法上非常的相似,但是两者的设计动机却没什么共同点;
这里讲的python中的decorators是对python中的function/method做装饰,这些修饰仅是当声明一个函数或者方法的时候,才会应用的额外调用。
python中的decorator分 ......

python31与C交互

1.c调用python:
   实例代码:
main.c调用test.py的
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//main.c
#include <windows.h>
......

python的C、c++扩展

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资源收集

Programming Python, 2nd Edition (O'Reilly)
http://www.osbbs.com/dl/Programming Python, 2nd Edition (O'Reilly).chm
很全很经典了python学习入门资料
OReilly - Learning Python:
http://www.osbbs.com/dl/OReilly - Learning Python.chm ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号