Advertisement
horozov86

iter_example

Jul 18th, 2023
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. class MyIterator:
  2.     def __init__(self, start, end):
  3.         self.start = start
  4.         self.end = end
  5.         self.current = self.start
  6.    
  7.    
  8.     def __iter__(self):
  9.         return self
  10.        
  11.     def __next__(self):
  12.         if self.current >= self.end:
  13.             raise StopIteration()
  14.         else:
  15.             self.current += 1
  16.             return self.current - 1
  17.            
  18. my_iterator = MyIterator(1, 11)
  19. for num in my_iterator:
  20.     print(num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement