{"record":{"id":"e92a7d3ef5a83bc3","repo":"krahets/hello-algo","slug":"error-e92a7d","errorCode":null,"errorMessage":"индекс выходит за границы","messagePattern":"индекс выходит за границы","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ru/codes/python/chapter_array_and_linkedlist/my_list.py","lineNumber":30,"sourceCode":"        \"\"\"Конструктор\"\"\"\n        self._capacity: int = 10  # Вместимость списка\n        self._arr: list[int] = [0] * self._capacity  # Массив (для хранения элементов списка)\n        self._size: int = 0  # Длина списка (текущее число элементов)\n        self._extend_ratio: int = 2  # Коэффициент увеличения списка при каждом расширении\n\n    def size(self) -> int:\n        \"\"\"Получить длину списка (текущее число элементов)\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"Получить вместимость списка\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"Доступ к элементу\"\"\"\n        # Если индекс выходит за границы, выбрасывается исключение; далее аналогично\n        if index < 0 or index >= self._size:\n            raise IndexError(\"индекс выходит за границы\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"Обновление элемента\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"индекс выходит за границы\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"Добавление элемента в конец\"\"\"\n        # При превышении вместимости по числу элементов запускается расширение\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"Вставка элемента в середину\"\"\"","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ru/codes/python/chapter_array_and_linkedlist/my_list.py#L12-L48","documentation":"This IndexError (Russian: 'индекс выходит за границы' = 'index out of bounds') is raised by MyList.get(index) (my_list.py:30, ru translation) when index < 0 or index >= self._size. MyList keeps the live element count in _size distinct from the backing array capacity, so it must validate bounds itself. get() performs no resizing and is a pure read, so the guard is the only protection against reading uninitialized backing slots.","triggerScenarios":"Calling get(index) with index outside [0, size). Reading get(size()) — the classic off-by-one (valid range is 0..size-1). Passing a negative index. Indexing by a value derived from capacity (10) rather than current size.","commonSituations":"Off-by-one loops `for i in range(size()+1)` that overshoot. Confusing capacity with size when the list is partially filled. Stale index after add/insert/remove changed _size. Using get() on an empty list.","solutions":["Pre-validate `0 <= index < lst.size()` before get().","Loop `for i in range(lst.size())` (exclusive upper bound), not size()+1.","Re-derive any cached index after mutations that change _size.","Use to_array() and Python's own list indexing if you need negative-index or slicing semantics."],"exampleFix":"// before\nfor i in range(lst.size() + 1):\n    x = lst.get(i)  # IndexError at i == size()\n\n// after\nfor i in range(lst.size()):\n    x = lst.get(i)","handlingStrategy":"validation","validationCode":"if 0 <= index < lst.size():\n    val = lst.get(index)\n# use size(), not capacity() — capacity (10) != element count","typeGuard":"def valid_get_index(lst: MyList, index: int) -> bool:\n    return isinstance(index, int) and 0 <= index < lst.size()","tryCatchPattern":"try:\n    val = lst.get(index)\nexcept IndexError:\n    val = None","preventionTips":["Bound-check against size() (element count), not capacity() (backing length).","Loop `for i in range(lst.size())` — exclusive upper bound, never size()+1.","Recompute any cached index after add/insert/remove change _size.","For negative-index or slice needs, use to_array() and Python's native list indexing."],"tags":["list","python","index-out-of-bounds","dynamic-array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}