-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28thClassIteratorsinPython.py
More file actions
63 lines (32 loc) · 1.14 KB
/
28thClassIteratorsinPython.py
File metadata and controls
63 lines (32 loc) · 1.14 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
57
58
59
60
61
62
63
# ! Class Iterators
# * __iter__ is basically the thing which is actually the iterator
# * __next__ is similar to that of step in range function
class MyContainer:
def __init__(self, mylist):
self.mylist = mylist
def __iter__(self): # * Returns the iterable thing to tt
self.i=0 # * We set iterable i here
return self
def __next__ (self):
self.i += 1
if self.i >= len(self.mylist):
raise StopIteration
return self.mylist[self.i - 1] # * We do -1 so that 1 - 1 happens to give us 0 so that we get from the start
tt = MyContainer([1,2,3,4,5])
for i in tt:
print(i)
class SquareNum:
def __init__(self, n):
self.n = n
self.current = 0
def __iter__(self): # * Returns the iterable thing to variable sq
return self
def __next__(self):
if self.current >= self.n:
raise StopIteration # * Stops the loop after condition is met
square = self.current ** 2 # * Square no. are printed
self.current += 1
return square
sq = SquareNum(5)
for i in sq:
print(i)