生成器也是一个迭代器,通过next()函数实现按需计算下一个数据
列表生成器,使代码变得更加简洁,只记录当前的位置,只有__next__(),
(i*2 for i in range(10))
生成器(generator)
1 def gene(time): 2 a, b = 0, 1 3 n = 0 4 while n < time: 5 yield b 6 a, b = b, a + b 7 n = n + 1; 8 9 10 g = gene(10) 11 print(g.__next__()) 12 print(g.__next__()) 13 print(g.__next__()) 14 print("============================") 15 for i in g: 16 print(i)