-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.iterators.py
More file actions
44 lines (32 loc) · 915 Bytes
/
18.iterators.py
File metadata and controls
44 lines (32 loc) · 915 Bytes
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
# Example 1 : Iterating Through an Iterator
print('>>>> Example - 1 >>>>')
my_list = [4, 7, 0, 3]
my_iter = iter(my_list)
print('my_iter 1 : ', next(my_iter))
print('my_iter 2 : ', next(my_iter))
print('my_iter 3 : ', next(my_iter))
print('my_iter 4 : ', next(my_iter))
# Example 2 : Building Custom Iterators - An iterator of powers of two.
print('\n')
print('>>>> Example - 2 >>>>')
class PowTwo:
def __init__(self, max_value=0):
self.max_value = max_value
def __iter__(self):
self.n = 0
return self
def __next__(self):
if self.n <= self.max_value:
result = 2 ** self.n
self.n += 1
return result
else:
raise StopIteration
numbers = PowTwo(3)
iter_obj = iter(numbers)
while True:
try:
item = next(iter_obj)
print('Custom iter items :', item)
except StopIteration:
break