例子: 1,1,2,3,5,8,13,21......
def fib(max):
n,a,b=0,0,1
while n<max:
#print(b)
yield(b)
a,b=b,a+b
n+=1
return 'done'
f=fib(10)
while True:
try:
x=next(f)
print("f:",x)
except StopIteration as e:
print('Generator return value:',e.value)
break
注意:a,b=b,a+b
相当于:
t=(b,a+b) # t 是一个元组
a=t[0]
b=t[1]
但不必显示写出t临时变量就可以赋值