0

What is the reason that a numpy array doesn't raise an out of boundary exception when accessed through an index interval?

That behavior really messes up when you're debugging. Here is a code snippet that perfectly works despite being wrong,

import numpy

myArray = 0

def fillArray():
    global myArray
    myArray = numpy.array([1,2,3,4])

def accessArray():
    global myArray
    print(myArray[0:len(myArray) + 99999])

def main():
    fillArray()
    accessArray()

main()
raullalves
  • 836
  • 8
  • 20
  • That's a question – raullalves May 30 '18 at 03:59
  • 5
    Because conventionally, slices of sequences in python do not raise errors. None of the built-in sequence types will raise an error with a slice that has a stop that is "out of bounds", and the developers of numpy chose to follow this convention. – juanpa.arrivillaga May 30 '18 at 03:59
  • Also, what is "wrong" about the code you posted? – juanpa.arrivillaga May 30 '18 at 04:00
  • Note also, slicing shouldn't really be thought of accessing. Think of it as shorthand for an operation that works across your sequence, that is definable by a start, stop, and step. – juanpa.arrivillaga May 30 '18 at 04:01
  • I'm trying to access the interval 0:len(myArray) + 99999, where its len is 4 – raullalves May 30 '18 at 04:01
  • 2
    Yes, what is wrong about that? There is well-defined behavior for how that works. That such behavior doesn't work like you *expect*, no doubt based on the behavior of some other language you are coming from, doesn't make the behavior "wrong" – juanpa.arrivillaga May 30 '18 at 04:05
  • 1
    To build on @juanpa.arrivillaga 's comments, by definition `my_array[a:b]` is a view of the elements of `my_array` whose indices are within `a` (incl.) and `b` (excl.), no matter whether or not there are such indices. If the slice is empty then so be it. – Julien May 30 '18 at 04:07
  • 1
    See https://stackoverflow.com/questions/22951107/why-pythons-list-slicing-doesnt-produce-index-out-of-bound-error and https://stackoverflow.com/questions/9490058/why-substring-slicing-index-out-of-range-works-in-python and https://stackoverflow.com/questions/40558529/why-does-assigning-past-the-end-of-a-list-via-a-slice-not-raise-an-indexerror?noredirect=1&lq=1 – PM 2Ring May 30 '18 at 04:08
  • You can read about the behavior of slicing with regards to sequence types [here in the docs](https://docs.python.org/3.6/library/stdtypes.html#sequence-types-list-tuple-range). Numpy simply follows these conventions, although in principle, there is nothing that would prevent the developers of numpy from making this throw an error. – juanpa.arrivillaga May 30 '18 at 04:10
  • [Antti's answer](https://stackoverflow.com/a/40560564/4014959) is quite good. The key point being: "The desirability of this is in that it doesn't need any _exceptions_". – PM 2Ring May 30 '18 at 04:12
  • Because of conventions like `alist[3:]` and `alist[:-3]` we don't usually need to specify the length of the list or array. The object already has that information (`len` or `shape`) – hpaulj May 30 '18 at 04:49

0 Answers0