forked from alamm4521/python_test_codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python_Iterators.py
134 lines (97 loc) · 3.4 KB
/
Python_Iterators.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#Python Iterators (with 70s show)
'''
Python Iterators
An iterator is an object that contains a countable number of values.
An iterator is an object that can be iterated upon, meaning that you can traverse through all the values.
Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().
'''
'''
Iterator vs Iterable
Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from.
All these objects have a iter() method which is used to get an iterator:
'''
mytuple = ("red", "bob", "pam", "kitty", "midiz")
miit = iter(mytuple)
print(next(miit))
print(next(miit))
print(next(miit))
print(next(miit))
print(next(miit))
#Even strings are iterable objects, and can return an iterator:
#Example
#Strings are also iterable objects, containing a sequence of characters:
print("#Strings are also iterable objects, containing a sequence of characters:")
mystr = "bananan"
miit = iter(mystr)
print(next(miit))
print(next(miit))
print(next(miit))
print(next(miit))
print(next(miit))
print(next(miit))
print(next(miit))
'''
Looping Through an Iterator
We can also use a for loop to iterate through an iterable object:
'''
mytuple = ("pam", "bob", "red","hyde")
for x in mytuple:
print(x)
'''
Example
Iterate the characters of a string:'''
mystr = "hi boys"
for x in mystr:
print(x)
#The for loop actually creates an iterator object and executes the next() method for each loop.
'''
Create an Iterator
To create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object.
As you have learned in the Python Classes/Objects chapter, all classes have a function called __init__(), which allows you to do some initializing when the object is being created.
The __iter__() method acts similar, you can do operations (initializing etc.), but must always return the iterator object itself.
The __next__() method also allows you to do operations, and must return the next item in the sequence.
'''
'''
Example
Create an iterator that returns numbers, starting with 1, and each sequence will increase by one (returning 1,2,3,4,5 etc.):
'''
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
x = self.a
self.a += 1
return x
myclass = MyNumbers()
myiter = iter(myclass)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
'''
StopIteration
The example above would continue forever if you had enough next() statements, or if it was used in a for loop.
To prevent the iteration from going on forever, we can use the StopIteration statement.
In the __next__() method, we can add a terminating condition to raise an error if the iteration is done a specified number of times:
'''
#Example
#Stop after 20 iterations:
class MyNumbers_001:
def ___iter__(self):
self.a = 1
return self
def __next__(self):
if self.a <= 5:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass_001 = MyNumbers_001()
myiter_001 = iter(myclass)
for x in myiter_001:
print(x)