-
Notifications
You must be signed in to change notification settings - Fork 0
/
video4.py
55 lines (42 loc) · 1.25 KB
/
video4.py
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
''' Topics : sequence, iterator, iterable, differences between them with code snippets'''
import math
my_list = [1, 2, 3, 4, 5] # list / iterable
print(hasattr(my_list, '__iter__'))
print(hasattr(my_list, '__next__'))
for ele in my_list:
print(ele)
my_iter = iter(my_list) # my_list.__iter__()
while True:
try:
print(next(my_iter)) # my_list.__next__()
except StopIteration:
print('We have reached end of the list')
break
class my_own_iterator:
"""Class about an iterator which performs i ** i for all i <= user_input"""
def __init__(self, max = 0):
self.max_ele = max
def __iter__(self):
self.ele = 1
return self
def __next__(self):
if self.ele <= self.max_ele:
result = self.ele ** self.ele
self.ele += 1
return result
else:
raise StopIteration
my_itr_obj = my_own_iterator(5)
my_itr = iter(my_itr_obj)
# print(next(my_itr))
# print(next(my_itr))
# print(next(my_itr))
# print(next(my_itr))
# print(next(my_itr))
# print(next(my_itr))
while True:
try:
print(next(my_itr))
except StopIteration:
print('We have reached the limit...')
break