-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathiter_tutorial.py
94 lines (66 loc) · 2.09 KB
/
iter_tutorial.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
from collections.abc import Iterable, Iterator
# The iter() method returns an iterator for the given object.
# The syntax of iter() method is
# iter(object[, sentinel])
# The iterator protocol
# Any class that provides an __iter__ method is iterable;
# that method must return an Iterator instance that will cover
# all the elements in that class.
# Since an iterator is already looping over elements,
# its __iter__ function traditionally return itself.
# Iterable - __iter__ (for in), iter(Iterable) -> Iterator
# Iterator - __next__ + __iter__
class PrintNumber:
def __init__(self, max_number):
self.max_number = max_number
def __iter__(self):
self.num = 0
return self
def __next__(self):
if self.num >= self.max_number:
raise StopIteration
self.num += 1
return self.num
def example_1():
print_mum = PrintNumber(3)
print_mum_iter = iter(print_mum)
# prints '1'
print(next(print_mum_iter))
# prints '2'
print(next(print_mum_iter))
# prints '3'
print(next(print_mum_iter))
# raises StopIteration
print(next(print_mum_iter))
class CapitalIterable:
def __init__(self, string):
self.string = string
def __iter__(self):
return CapitalIterator(self.string)
class CapitalIterator:
def __init__(self, string):
self.words = [w.capitalize() for w in string.split()]
self.index = 0
def __next__(self):
if self.index == len(self.words):
raise StopIteration()
word = self.words[self.index]
self.index += 1
return word
def __iter__(self):
return self
def example_2():
iterable = CapitalIterable('the aa bb cc dd')
print('isinstance(iterable,Iterable):', isinstance(iterable, Iterable))
iterator = iter(iterable)
print('isinstance(iterator,Iterator):', isinstance(iterator, Iterator))
while True:
try:
print(next(iterator))
except StopIteration:
break
for i in iterable:
print(i)
if __name__ == "__main__":
example_1()
# example_2()