-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterators.py
88 lines (67 loc) · 1.67 KB
/
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
import constants as Site
DZ = Site.Core()
DZ.line("DZ module initiated")
#import inspect
import pprint
#########################DZ.line("HEADER ##########################
DZ.line("Iterating through an iterator")
DZ.line("define list")
my_list = [4,7,0,3]
DZ.line("get an iterator using iter()")
my_iter = iter(my_list)
DZ.line("iterate through it using next()")
DZ.line("output: 4")
print(next(my_iter))
DZ.line("output: 7")
print(next(my_iter))
DZ.line("next(obj) is same as obj.__next__()")
DZ.line("Output: 0")
print(my_iter.__next__())
DZ.line("Output: 3")
print(my_iter.__next__())
#DZ.line("This will raise an error, no items left")
#next(my_iter)
# custom iterator #
class PowTwo:
"""Class to implement an iterator
of powers of two"""
def __init__(self, max=0):
self.max = max
def __iter__(self):
self.n = 0
return self
def __next__(self):
if self.n <= self.max:
result = 2 ** self.n
self.n += 1
return result
else:
raise StopIteration
DZ.line("create an object")
numbers = PowTwo(3)
DZ.line("create an iterable from the object")
i = iter(numbers)
DZ.line("Using next to get to the next iterator element")
print(next(i))
print(next(i))
print(next(i))
DZ.line("Now for some infinite iterators")
int()
inf = iter(int,1)
next(inf)
next(inf)
class InfIter:
"""Infinite iterator to return all
odd numbers"""
def __iter__(self):
self.num = 1
return self
def __next__(self):
num = self.num
self.num += 2
return num
a = iter(InfIter())
print(next(a))
print(next(a))
print(next(a))
print(next(a))