3 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
__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貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊
產(chǎn)量
def count(n=0): while True: yield n n += 1
gen = (n for n in xrange(0,11))
添加回答
舉報(bào)