#列表生成式 list1 = [i*2 for i in range(1,11)] #生成器,在调用时才会生成相应的数据 list = (x*2 for x in range(1,1000000)) #list.__next__() #函数生成器 def fib(max): n,a,b = 0,0,1 while n < max: #print (b) yield b a,b = b,a+b n = n + 1 #return异常的时候打印消息 return "done" g = fib(10) while True: try: x = next(g) print ("g:",x) except StopIteration as e: print ("Generator return value:",e.value) break
时间: 2024-12-19 22:41:23