def init(self):
print(f"Cities init called.id(self) = {id(self)}")
self._cities = ["Paris", "Berlin", "Rome", "Madrid", "London"]
self._index = 0
def len(self):
return len(self._cities)
def iter(self):
print("Cities iter called.")
self._old_iter = CityIterator(self)
return self._old_iter
class CityIterator:
def init(self, _city_obj):
print(f"CityIterator init called. id(self) = {id(self)} type(self)={type(self)}")
self._city_obj = _city_obj
self._index = 0
def iter(self):
print("CityIterator iter called.")
return self
def next(self):
print("CityIterator next called.")
if self._index >= len(self._city_obj):
raise StopIteration
else:
item = self._city_obj._cities[self._index]
self._index += 1
return item
cities = Cities()
for city in cities:
print(city)
for city in cities:
print(city)
Эээ, а что изменилось?
Обсуждают сегодня