-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.iterators.py
More file actions
60 lines (47 loc) · 1.4 KB
/
15.iterators.py
File metadata and controls
60 lines (47 loc) · 1.4 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
# Iterators
'''
1. iterater is use to perform iteration
2. for perfmoring iteration we have 2 objects __iter__() and __next__()
Meaing of iteration:
1. in this case we use list,tuple,set and dict
'''
for num in range(10):
print(num)
# using iter and next
listForIter = ['ravi','ram','shyam']
print('List for iter values: ',listForIter)
# first need to use iter methods to get the values
myit = iter(listForIter)
print('\n\nnext value: ',next(myit))
print('\n\nnext value: ',next(myit))
print('\n\nnext value: ',next(myit))
# print('\n\nnext value: ',next(myit))
# normal class
class MyClass:
# def __init__(self, number):
# self.n = number
# def outpute(self):
# print('\nTHis is number: ',self.n)
# first creat iter
def __iter__(self):
self.n = 1
# for passing self value to next must resturn
return self
def __next__(self):
# condtion to stop next at one point
if self.n < 10:
# fist to recive n value from iter
n = self.n
# now must to incremtnt the slef.n value
self.n += 1
return n
else:
raise StopIteration
# finally cret object of class
myiterClassObj = MyClass()
myiterval = iter(myiterClassObj)
# print('My iter value: ',next(myiterval))
for newnum in myiterval:
print('\nNewnum values: ',newnum)
# myobj = MyClass(10)
# myobj.outpute()