3 回答
TA贡献1863条经验 获得超2个赞
__iter__()
next()
__iter__
next()
next()
class Counter: def __init__(self, low, high): self.current = low self.high = high def __iter__(self): return self def next(self): # Python 3: def __next__(self) if self.current > self.high: raise StopIteration else: self.current += 1 return self.current - 1for c in Counter(3, 8): print c
3 4 5 6 7 8
def counter(low, high): current = low while current <= high: yield current current += 1for c in counter(3, 8): print c
TA贡献1799条经验 获得超6个赞
产量
def count(n=0): while True: yield n n += 1
gen = (n for n in xrange(0,11))
添加回答
举报