Skip to content

Commit fe9ae5d

Browse files
committed
Fix Iterator pattern
1 parent 5813be0 commit fe9ae5d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/Iterator/Conceptual/main.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from __future__ import annotations
1515
from collections.abc import Iterable, Iterator
16-
from typing import Any, List
16+
from typing import Any
1717

1818

1919
"""
@@ -60,7 +60,7 @@ def __init__(self, collection: WordsCollection, reverse: bool = False) -> None:
6060
self._reverse = reverse
6161
self._position = -1 if reverse else 0
6262

63-
def __next__(self):
63+
def __next__(self) -> Any:
6464
"""
6565
EN: The __next__() method must return the next item in the sequence. On
6666
reaching the end, and in subsequent calls, it must raise StopIteration.
@@ -87,8 +87,12 @@ class WordsCollection(Iterable):
8787
получения новых экземпляров итератора, совместимых с классом коллекции.
8888
"""
8989

90-
def __init__(self, collection: List[Any] = []) -> None:
91-
self._collection = collection
90+
def __init__(self, collection: list[Any] | None = None) -> None:
91+
self._collection = collection or []
92+
93+
94+
def __getitem__(self, index: int) -> Any:
95+
return self._collection[index]
9296

9397
def __iter__(self) -> AlphabeticalOrderIterator:
9498
"""
@@ -98,10 +102,10 @@ def __iter__(self) -> AlphabeticalOrderIterator:
98102
RU: Метод __iter__() возвращает объект итератора, по умолчанию мы
99103
возвращаем итератор с сортировкой по возрастанию.
100104
"""
101-
return AlphabeticalOrderIterator(self._collection)
105+
return AlphabeticalOrderIterator(self)
102106

103107
def get_reverse_iterator(self) -> AlphabeticalOrderIterator:
104-
return AlphabeticalOrderIterator(self._collection, True)
108+
return AlphabeticalOrderIterator(self, True)
105109

106110
def add_item(self, item: Any):
107111
self._collection.append(item)

0 commit comments

Comments
 (0)