Advertisement
Guest User

Iterator class with steps

a guest
Feb 6th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. # Try writing your own iterator class that allows you
  2. # to specify the lengths of steps the iterator makes.
  3. # eg. when you call you step iterator class you specify the steps.
  4.  
  5. class IterSteps:
  6.     def __init__(self,data,step):
  7.         self.index = 0
  8.         self.data = data
  9.         self.indexes = list(range(0,step,len(data)))
  10.         self.step = step
  11.     def __iter__(self):
  12.         return self
  13.     def __next__(self):
  14.         if self.index > len(self.data) - 1:
  15.             raise StopIteration
  16.         self.index += self.step
  17.         return self.data[self.index - self.step]
  18.  
  19. def main():
  20.     itstep = IterSteps("Pikachu",3)
  21.     for thing in itstep:
  22.         print(thing)
  23.        
  24. if __name__ == "__main__":
  25.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement