• # python_ganerator_learning——2

时间：2022-01-01 23:34:19      阅读：434      评论：0      收藏：0      [点我收藏+]
原文：https://www.cnblogs.com/ttm6489/p/15756255.html
`# author: Roy.G# a=[2*i  for i in range(10)] #this is a list# a=print(a)# b=(2*i  for i in range(1000)) #this is a ganerator# # print(b)# # for i in b:# #     print(i)# a=0# ## for i in b:#     print(i)#     a=a+1#     print(a)#     if a>3:#         break# print(b.__next__())# print(b.__next__())# print(b.__next__())# print(b.__next__())# print(b.__next__())# print(b.__next__())# # print(b.__next__())def fib(max):  #fibonacci series    n,a,b=0,0,1    while n<max:        # print(b)        yield b   # yield interrupt zhe function and out put data orderly        t=[b,a+b]        a=t[0]        b=t[1]        # a1=b        # b=a+b        # a=a1        # a,b=b,a+b        n=n+1    return "done!"# print(fib(9))# for i in fib(10):#     print(i)# print("-----")f=fib(10)print("===>next(f)",next(f))print("===>next(f)",next(f))print("===>next(f)",next(f))print("===>next(f)",next(f))print("===>next(f)",next(f))print("===>next(f)",next(f))print("===>next(f)",next(f))# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())# print(f.__next__())print("===============wrong way==every time of printing fib ,means restart fib again,so outpu is 1 every time===============")# print(fib(100).__next__())  #every time of printing fib ,means restart fib again,so outpu is 1 every time# print(fib(100).__next__())# print(fib(100).__next__())# print(fib(100).__next__())# print(fib(100).__next__())while True:    try:        x=next(f)        print("f_next",x)    except StopIteration as e:        print("generator return value:",e.value)        break`

标签：

0
• 评论
• 一句话评论（0
暂无评论！
登录后才能评论！
• 教程昨日排行
•