Guest User

Untitled

a guest
Mar 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. class CitiesManager(object):
  2. """Класс, содержащий функциональность работы с городами."""
  3.  
  4. def __init__(self, cities):
  5. self.cities = cities
  6.  
  7. def __getitem__(self, item: int):
  8. """Получить элемент по индексу.
  9.  
  10. Args:
  11. item (int): индекс элемента для получения в операциях среза.
  12. """
  13. return self.cities[item]
  14.  
  15. def __len__(self):
  16. return len(self.cities)
  17.  
  18. def __iter__(self):
  19. """Возвратить себя как объект итератора."""
  20. return self
  21.  
  22. def __next__(self):
  23. """Получить следующий элемент """
  24. for item in self.cities:
  25. yield item
  26.  
  27. cities = ['Петрозаводск', 'Хельсинки', 'Санкт-Петербург']
  28. north_cities = CitiesManager(cities)
  29.  
  30.  
  31. for city in north_cities:
  32. print(city)
  33.  
  34. <generator object __next__ at 0x7f13de432d58>
  35. <generator object __next__ at 0x7f13de432c50>
  36. <generator object __next__ at 0x7f13de432d58>
  37. <generator object __next__ at 0x7f13de432c50>
  38. <generator object __next__ at 0x7f13de432d58>
Add Comment
Please, Sign In to add comment