生成器是一种特殊的迭代器,它有yield语句

#coding:utf-8
def fibs(max):
n,a,b = 0,0,1
while n < max:
yield b
a , b = b ,a + b
n = n + 1
if __name__=="__main__":
f = fibs(10)
for i in f:
print i

output:

1
1
2
3
5
8
13
21
34
55