# 求斐波那数列 :1,1,2,3,5,8,13,21,34......
'''
函数生成器,关键字 yield
'''
def get_fbn(max_num):
a = 0
b = 1
c = list()
while b <= max_num:
tmp = a
a = b
b += tmp
c.append(a)
yield c # 返回c,并且暂停当前函数
if __name__ == '__main__':
func = get_fbn(10) # 下面每执行一次func,则继续从get_fbn上次暂停位置继续执行
print(func.__next__()) # 输出:[1]
print('xxxxxxx')
print(func.__next__()) # 输出:[1, 1]
print('xxxxxxx')
print(func.__next__()) # 输出:[1, 1, 2]
print('xxxxxxx')
print(func.__next__()) # 输出:[1, 1, 2, 3]
print('xxxxxxx')
print(func.__next__()) # 输出:[1, 1, 2, 3, 5]
print('xxxxxxx')
print(func.__next__()) # 输出:[1, 1, 2, 3, 5, 8]
来源:oschina
链接:https://my.oschina.net/jugier/blog/4284512