Day4-迭代器生成器

#生成器只有在调用的时候才生成数据, yield 
#只记得住当前的位置
#列表生成器
# a = (i*2 for i in range(10000))
# print(a.__next__())
# for i in range(10):
# a.append(i*2)

# a , = b, a+b的解释如下
# t = (b, a+b)
# a = t[0]
# b = t[1]
#斐波拉契
def fib(max):
n,a,b = 0,0,1
while n < max:
#print(b)
yield b
a,b = b,a+b
#a , = b, a+b的解释如下
# t = (b, a+b)
# a = t[0]
# b = t[1]
n=n+1
return "done"
#fib(10)
g = fib(10)
# print(f.__next__())
# print("--------------")
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
while True:
try:
x = next(g)
print("g:",x)
except StopIteration as e:
print("Generator return value:", e.value)
break

猜你喜欢

转载自www.cnblogs.com/carol7258/p/13177990.html
今日推荐