As described previously, an iterator is an abstract concept that encapsulates the idea of walking through each element of an array. The basic pseudocode for an iterator-based loop used in NumPy is:
set up iterator (including pointing the current value to the first value in the array) while iterator not done:
process the current valuepoint the current value to the next value
process the current value
must be handled by the iterator and deserves discussion.
As a result, there are basically three parts to the iterator design:
Moving to the next value
These will each be discussed separately. The design considerations that went into NumPy's iterators included making the overhead for using them inside of a loop as small as possible and making them as fast as possible.
The first decision is the order in which the elements will be taken. Although one could conceive of an iterator with no guarantee of the order in which the elements are taken, it is useful most of the time for the programmer to know the order. As a result, iterators in NumPy follow a specific order. The order is obtained using a relatively simple approach patterned after simple counting (with wrap-around) using a tuple of digits. Let a tuple of N integers represent the current position in the array, with (0,…,0) representing the first element of the n1 x n2 x … x nN array, and (n1−1, n2−1,…, nN−1) representing the last element.
This tuple of integers represents ...