Advertisement
horozov86

dictionary_iterator

Jul 19th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. class dictionary_iter:
  2.     def __init__(self, dictionary):
  3.         self.dictionary = dictionary
  4.         self.items = list(dictionary.items()) # речникът се обръща на масив от тюпъли
  5.         self.idx = 0 # създаваме допълнителен атрибут индекс
  6.        
  7.        
  8.     def __iter__(self):
  9.         return self
  10.        
  11.     def __next__(self):
  12.         if self.idx >= len(self.items): # условие за пркратяване, ако индекса стане по-голям от дължината на масива!!!
  13.             raise StopIteration
  14.         else:
  15.             result = self.items[self.idx]
  16.             self.idx += 1
  17.             return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement