|
发表于 2022-7-29 00:55:45
|
显示全部楼层
本帖最后由 suchocolate 于 2022-7-29 01:03 编辑
__next__里定死了3,外面用10没有意义。不如这样:
- class Iter:
- def __init__(self, limit=1):
- self.count = 0
- self.limit = limit
- def __iter__(self):
- return self
- def __next__(self):
- self.count += 1
- if self.count > self.limit:
- raise StopIteration
- return self.count
- a = Iter(3)
- try:
- while True:
- print(next(a))
- except Exception as e:
- print(e)
- print('=' * 20)
- a = Iter(10)
- try:
- while True:
- print(next(a))
- except Exception as e:
- print(e)
-
复制代码 |
评分
-
查看全部评分
|