正反向迭代:
class iterRange(object): def __init__(self, start, end, step): self.start = start self.end = end self.step =step def __iter__(self): v = self.start while v <= self.end: yield v v += self.step def __reversed__(self): v = self.end while v >= self.start: yield v v -= self.step print('正向', 20*'-') for f in iterRange(1.0, 5.0, 0.5): print(f) print('反向', 20*'-') for f in reversed(iterRange(1.0, 5.0, 0.5)): print(f)
转载于:https://www.cnblogs.com/ray-mmss/p/10419333.html