Advertisement
horozov86

reverse_iter

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