class MyRange:
def __init__(self, start, end):
self.value = start
self.end = end
def __iter__(self):
return self
def __next__(self):
#若当前值大于最后值,捕获 StopIteration 异常
if self.value >= self.end:
raise StopIteration
current = self.value
self.value += 1
return current
for num in MyRange(1,4):
print(num)
网友评论