EN: Concrete Collections provide one or several methods for retrieving fresh iterator instances, compatible with the collection class. RU: Конкретные Коллекции предоставляют один или несколько методов для получения новых экземпляров итератора, совместимых с классом коллекции.
| 89 | |
| 90 | |
| 91 | class WordsCollection(Iterable): |
| 92 | """ |
| 93 | EN: Concrete Collections provide one or several methods for retrieving fresh |
| 94 | iterator instances, compatible with the collection class. |
| 95 | |
| 96 | RU: Конкретные Коллекции предоставляют один или несколько методов для |
| 97 | получения новых экземпляров итератора, совместимых с классом коллекции. |
| 98 | """ |
| 99 | |
| 100 | def __init__(self, collection: list[Any] | None = None) -> None: |
| 101 | self._collection = collection or [] |
| 102 | |
| 103 | |
| 104 | def __getitem__(self, index: int) -> Any: |
| 105 | return self._collection[index] |
| 106 | |
| 107 | def __iter__(self) -> AlphabeticalOrderIterator: |
| 108 | """ |
| 109 | EN: The __iter__() method returns the iterator object itself, by default |
| 110 | we return the iterator in ascending order. |
| 111 | |
| 112 | RU: Метод __iter__() возвращает объект итератора, по умолчанию мы |
| 113 | возвращаем итератор с сортировкой по возрастанию. |
| 114 | """ |
| 115 | return AlphabeticalOrderIterator(self) |
| 116 | |
| 117 | def get_reverse_iterator(self) -> AlphabeticalOrderIterator: |
| 118 | return AlphabeticalOrderIterator(self, True) |
| 119 | |
| 120 | def add_item(self, item: Any) -> None: |
| 121 | self._collection.append(item) |
| 122 | |
| 123 | |
| 124 | if __name__ == "__main__": |