티스토리 뷰


# Python 3 : Iterator


class CountDown:
def __init__(self, top: int):
self.current = top + 1

def __iter__(self) -> 'CountDown':
return self

def __next__(self) -> int:
self.current -= 1
if self.current < 0:
raise StopIteration
else:
return self.current


if __name__ == '__main__':
c1 = CountDown(3)
print(type(c1)) # <class '__main__.CountDown'>

for i in c1:
print(i) # 3 2 1 0

c2 = CountDown(3)
print(next(c2)) # 3
print(next(c2)) # 2
print(next(c2)) # 1
print(next(c2)) # 0
print(next(c2)) # StopIteration

""" Output
3
2
1
0
3
2
1
0
StopIteration
"""


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함