# 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
本文摘自 :https://www.cnblogs.com/