13
13
14
14
from __future__ import annotations
15
15
from collections .abc import Iterable , Iterator
16
- from typing import Any , List
16
+ from typing import Any
17
17
18
18
19
19
"""
@@ -60,7 +60,7 @@ def __init__(self, collection: WordsCollection, reverse: bool = False) -> None:
60
60
self ._reverse = reverse
61
61
self ._position = - 1 if reverse else 0
62
62
63
- def __next__ (self ):
63
+ def __next__ (self ) -> Any :
64
64
"""
65
65
EN: The __next__() method must return the next item in the sequence. On
66
66
reaching the end, and in subsequent calls, it must raise StopIteration.
@@ -87,8 +87,12 @@ class WordsCollection(Iterable):
87
87
получения новых экземпляров итератора, совместимых с классом коллекции.
88
88
"""
89
89
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 ]
92
96
93
97
def __iter__ (self ) -> AlphabeticalOrderIterator :
94
98
"""
@@ -98,12 +102,12 @@ def __iter__(self) -> AlphabeticalOrderIterator:
98
102
RU: Метод __iter__() возвращает объект итератора, по умолчанию мы
99
103
возвращаем итератор с сортировкой по возрастанию.
100
104
"""
101
- return AlphabeticalOrderIterator (self . _collection )
105
+ return AlphabeticalOrderIterator (self )
102
106
103
107
def get_reverse_iterator (self ) -> AlphabeticalOrderIterator :
104
- return AlphabeticalOrderIterator (self . _collection , True )
108
+ return AlphabeticalOrderIterator (self , True )
105
109
106
- def add_item (self , item : Any ):
110
+ def add_item (self , item : Any ) -> None :
107
111
self ._collection .append (item )
108
112
109
113
0 commit comments