迭代器示例
erhuabushuo
posted @ 2011年11月14日 14:36
in Python
, 1053 阅读
#/usr/bin/env python3.2
class MyIterator():
def __init__(self, step):
self.step = step
def __next__(self):
"""Returns the next element."""
if self.step == 0:
raise StopIteration
self.step -= 1
return self.step
def __iter__(self):
"""Returns the iterator itself."""
return self
for el in MyIterator(5):
print(el, end=" ")
console:
4 3 2 1 0