-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiterator_3.py
More file actions
49 lines (39 loc) · 1.1 KB
/
iterator_3.py
File metadata and controls
49 lines (39 loc) · 1.1 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
#!/usr/bin/env python
"""
Simple iterator examples
"""
class IterateMe_3(object):
"""
Almost a replacement for xrange:
Iterate_2 (start, stop, step=1)
returns the sequence of numbers from start (inclusive) to stop (exclusive),
skipping every step number
( like xrange(start, stop, step) )
This version re-sets itself when used again.
"""
def __init__(self, start, stop, step=1):
self.current = self.start = start
self.stop = stop
self.step = step
def __iter__(self):
self.current = self.start
return self
def next(self):
if self.current < self.stop:
self.current += self.step
return self.current
else:
raise StopIteration
if __name__ == "__main__":
print "Test the usual"
for i in IterateMe_3(3, 11, 2):
print i
print "This one is different when broken out of"
it = IterateMe_3(3, 11, 2)
for i in it:
if i > 8:
break
print i
print "we pick up again from the beginning"
for i in it:
print i