{"record":{"id":"cab1767f666f8726","repo":"krahets/hello-algo","slug":"error-cab176","errorCode":null,"errorMessage":"堆疊為空","messagePattern":"堆疊為空","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/python/chapter_stack_and_queue/array_stack.py","lineNumber":30,"sourceCode":"        \"\"\"建構子\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"獲取堆疊的長度\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"判斷堆疊是否為空\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"入堆疊\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"出堆疊\"\"\"\n        if self.is_empty():\n            raise IndexError(\"堆疊為空\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"訪問堆疊頂元素\"\"\"\n        if self.is_empty():\n            raise IndexError(\"堆疊為空\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"返回串列用於列印\"\"\"\n        return self._stack\n\n\n\"\"\"Driver Code\"\"\"\nif __name__ == \"__main__\":\n    # 初始化堆疊\n    stack = ArrayStack()\n","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/python/chapter_stack_and_queue/array_stack.py#L12-L48","documentation":"Raised by the pop() method of an array-backed stack when size() returns 0. The stack delegates to Python's built-in list.append/pop for storage but adds an explicit emptiness guard before calling self._stack.pop(). This provides a descriptive error message instead of Python's generic 'pop from empty list'.","triggerScenarios":"Calling pop() on a newly constructed ArrayStack; calling pop() more times than push() in a processing loop; draining the stack in a while loop without checking is_empty().","commonSituations":"Expression-evaluation or backtracking algorithms that pop past empty; undo/redo stacks where redo is called with nothing to redo; testing boundary conditions with empty stacks.","solutions":["Check stack.is_empty() before calling pop()","Use while not stack.is_empty() as the draining loop condition","Wrap in try/except IndexError for defensive stack operations"],"exampleFix":"# before\nval = stack.pop()\n\n# after\nif not stack.is_empty():\n    val = stack.pop()\nelse:\n    val = None","handlingStrategy":"validation","validationCode":"if not stack.is_empty():\n    val = stack.pop()\nelse:\n    val = None","typeGuard":null,"tryCatchPattern":"try:\n    val = stack.pop()\nexcept IndexError:\n    val = None","preventionTips":["Check is_empty() before pop() in all stack-draining loops","Use while not stack.is_empty() as the loop condition","Track the number of push() calls for batch pop() operations"],"tags":["data-structure","stack","python","array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}