File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
students/alinafe/session10 Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+
3+ """
4+ Simple iterator examples
5+ """
6+
7+ class IterateMe_1 (object ):
8+ """
9+ About as simple an iterator as you can get:
10+
11+ returns the sequence of numbers from zero to 4
12+ ( like range(4) )
13+ """
14+ def __init__ (self , stop = 5 ):
15+ self .current = - 1
16+ self .stop = stop
17+ def __iter__ (self ):
18+ return self
19+ def __next__ (self ):
20+ self .current += 1
21+ if self .current < self .stop :
22+ return self .current
23+ else :
24+ raise StopIteration
25+
26+ if __name__ == "__main__" :
27+
28+ print ("Testing the iterator" )
29+ for i in IterateMe_1 ():
30+ print (i )
31+
32+
33+ class IterateMe_2 :
34+ """
35+ add three input parameters: iterator_2(start, stop, step=1)
36+
37+ """
38+
39+ def __init__ (self , start , stop = None , step = 1 ):
40+ self .current = - 1
41+ self .start = start
42+
43+ def __iter__ (self ):
44+ self .current = - 1
45+ print ("in __iter__" )
46+ return self
47+
48+ def __next__ (self ):
49+ self .current += 1
50+ if self .current < self .stop :
51+ return self .current
52+ else :
53+ raise StopIteration
54+
55+ if __name__ == "__main__" :
56+
57+ print ("Testing the iterator" )
58+ it = IterateMe_2 (2 , 10 , 2 )
59+
60+ for i in IterateMe_2 (20 ):
61+ print (i )
62+
You can’t perform that action at this time.
0 commit comments