-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiterator.py
More file actions
43 lines (32 loc) · 779 Bytes
/
iterator.py
File metadata and controls
43 lines (32 loc) · 779 Bytes
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
#!/usr/bin/env python
"""
迭代模式
"""
class Iter:
def __init__(self, data):
self.__index = 0
self.data = data
def begin(self):
self.__index = 0
def end(self):
self.__index = len(self.data)
def prev(self):
self.__index -= 1
def next(self):
if len(self.data) > self.__index:
self.__index += 1
return True
else:
return False
def get_current_data(self):
return self.data[self.__index - 1]
if __name__ == "__main__":
obj = Iter(data=list(range(10)))
while (obj.next()):
print(obj.get_current_data())
print(obj.get_current_data())
obj.prev()
obj.prev()
obj.prev()
obj.prev()
print(obj.get_current_data())