Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CitiesManager(object):
- """Класс, содержащий функциональность работы с городами."""
- def __init__(self, cities):
- self.cities = cities
- def __getitem__(self, item: int):
- """Получить элемент по индексу.
- Args:
- item (int): индекс элемента для получения в операциях среза.
- """
- return self.cities[item]
- def __len__(self):
- return len(self.cities)
- def __iter__(self):
- """Возвратить себя как объект итератора."""
- return self
- def __next__(self):
- """Получить следующий элемент """
- for item in self.cities:
- yield item
- cities = ['Петрозаводск', 'Хельсинки', 'Санкт-Петербург']
- north_cities = CitiesManager(cities)
- for city in north_cities:
- print(city)
- <generator object __next__ at 0x7f13de432d58>
- <generator object __next__ at 0x7f13de432c50>
- <generator object __next__ at 0x7f13de432d58>
- <generator object __next__ at 0x7f13de432c50>
- <generator object __next__ at 0x7f13de432d58>
Add Comment
Please, Sign In to add comment