Skip to content

Commit ae010ec

Browse files
committed
Behavorial pattern => Iterator
1 parent e7ff885 commit ae010ec

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

iterator.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Behavioral pattern:
3+
Iterator => 1.Iterable 2.Iteration
4+
5+
Requirements that should know:
6+
__iter__ , __next__
7+
"""
8+
9+
10+
class Iteration:
11+
def __init__(self, value):
12+
self.value = value
13+
14+
def __next__(self):
15+
if self.value == 0:
16+
raise StopIteration('End of sequence')
17+
for item in range(0, self.value):
18+
value = self.value
19+
self.value -= 1
20+
return value
21+
22+
23+
class Iterable:
24+
def __init__(self, value):
25+
self.value = value
26+
27+
def __iter__(self):
28+
return Iteration(self.value)
29+
30+
31+
if __name__ == '__main__':
32+
f1 = Iterable(5)
33+
f2 = iter(f1)
34+
35+
print(next(f2))
36+
print(next(f2))
37+
print(next(f2))
38+
print(next(f2))
39+
print(next(f2))
40+
"""we add raise error that don't show later than zero """
41+
print(next(f2))

0 commit comments

Comments
 (0)