1313
1414from __future__ import annotations
1515from 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,12 +102,12 @@ 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
106- def add_item (self , item : Any ):
110+ def add_item (self , item : Any ) -> None :
107111 self ._collection .append (item )
108112
109113
0 commit comments