Data Structures and Algorithms
with Object-Oriented Design Patterns in Python|
While the Python programming language does indeed provide built-in support for lists, that support is not without its shortcomings: For example, the first (or leftmost) index of a list is always zero (or -n) and the rightmost index of a list is always n-1 (or -1). However, in certain applications, it is desireable to index list elements starting with a non-zero base index. Another issue arise from the fact that negative indices are allowed. In many applications, only non-negative indices are required and an attempt to use a negative index is evidence of a programming error. Because Python allows negative indices, such errors are not detected at run time and may lead to incorrect program execution.
One way to address these deficiencies is to define a new class with the desired functionality. We do this by defining an Array class with two instance attributes, _data and _baseIndex. The first is a Python list and the second is a plain integer which records the lower bound for array indices.