# yield
def foo():
yield 1
yield 2
yield 9999
a = foo()
for i in a:
print i
>>> 1
2
9999
实例:
readline to readlines
def xreadlines():
seek=0
while 1:
with open('file.txt','r+') as f:
data = f.readline()
if data:
seek = f.tell()
yield data
else:
return
a = xreadlines()
type(a)
for i in a:
print i
# 三元运算
在C中
if x>y ?tmp= a:b
在Python中
tmp = a if x>y else b
# with ... as...
#lambda x,y:x+y