python 生成器一共两种创建方法:
1,(x for x in range(5))
2,yield
# vim 3.py
def fib(max):
n,before,after = 0,0,1
while n < max:
yield before
before,after = after,before+after
n += 1
g = fib(8)
print (next(g))
print (next(g))
print (next(g))
print (next(g))
print (next(g))
print (next(g))
print (next(g))
print (next(g))
[root@localhost python]# python 3.py
0
1
1
2
3
5
8
13
# vim 4.py def bar(): print ("ok1") #1 count = yield 1 print (count) yield 2 b = bar() next(b) ret = b.send("eeeeeeeeeeeeeeeee") print (ret) 运行结果: [root@localhost python]# python 4.py ok1 eeeeeeeeeeeeeeeee 2 看现象。。。。。