-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.py
More file actions
56 lines (45 loc) · 1.44 KB
/
iterator.py
File metadata and controls
56 lines (45 loc) · 1.44 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
it = [1, 2, 3].__iter__()
it.__next__()
1
it.__next__()
2
it.__next__()
3
it.__next__()
# 이터레이터 만들기
class Counter:
def __init__(self, stop):
self.current = 0 # 현재 숫자 유지, 0부터 지정된 숫자 직전까지 반복
self.stop = stop # 반복을 끝낼 숫자
def __iter__(self):
return self # 현재 인스턴스를 반환
def __next__(self):
if self.current < self.stop: # 현재 숫자가 반복을 끝낼 숫자보다 작을 때
r = self.current # 반환할 숫자를 변수에 저장
self.current += 1 # 현재 숫자를 1 증가시킴
return r # 숫자를 반환
else: # 현재 숫자가 반복을 끝낼 숫자보다 크거나 같을 때
raise StopIteration # 예외 발생
for i in Counter(3):
print(i, end=' ')
# 이터레이터 언패킹
a, b, c = Counter(3)
print(a, b, c)
a, b, c, d, e = Counter(5)
print(a, b, c, d, e)
# 인덱스로 접근할 수 있는 이터레이터 만들기
class Counter:
def __init__(self, stop):
self.stop = stop
def __getitem__(self, index):
if index < self.stop:
return index
else:
raise IndexError
print(Counter(3)[0], Counter(3)[1], Counter(3)[2])
for i in Counter(3):
print(i, end=' ')
it = iter(range(3))
next(it)
next(it)
next(it)