__author__ = ‘hechangting‘ #ecoding=utf-8 import itertools #迭代器 class Fib: def __init__(self): self.prev = 0 self.curr = 1 def __iter__(self): return self def __next__(self): value = self.curr self.curr += self.prev self.prev = value return value #生成器 class fib1: def fib(self,times): n=0 a,b=0,1 while n<times: yield b a,b=b,a+b n+=1 return ‘done‘ for n in fib1().fib(5): print(n) print("="*50) f=Fib() print(list(itertools.islice(f,0,2)))
原文地址:https://www.cnblogs.com/HCT118/p/8466114.html
时间: 2024-11-08 23:41:47