Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

python - List insert at index that is well out of range - behaves like append

I had a list

 a = [1, 2, 3]

when I did

a.insert(100, 100)

[1, 2, 3, 100]

as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed .

Should it not throw

IndexError: list assignment index out of range

exception as it throws when I attempt doing

a[100] = 100

Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ?

Personal Opinion :

Lets see how other languages behave in such a situation :

Ruby :

    > a = [1, 2]
    > a[100] = 100
    > a
 => [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100] 

The way ruby handles this is pretty clear and sounds meaningful at least to me .

Java :

In java the method .add(index, value) if applied with index that is out of range(on arraylist , linkedlist for example) will throw java.lang.IndexOutOfBoundsException .

So what I felt was either it should throw exception(as java does) or insert null in the range in between (as ruby handles it ). But the silent way of handling in python is just flummoxing .

UPDATE (16 Sep 2014 IST 8:30 AM) :

As suggested by one of the answerers I posted this question in python-dev and I got a response . It can be seen in this python dev mailing list thread . If you find that the thread link has changed, you can find the answer by doing a google search for the title of question appended at the beginning with python dev.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the docs:

list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

So technically when you're doing a.insert(100, 100) it ensures that 100 will be inserted at a index before 100 which happens to be, in this case, index 3.

Further, we can have a look at the implementation:

static int
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
{
    Py_ssize_t i, n = Py_SIZE(self);
    PyObject **items;
    if (v == NULL) {
        PyErr_BadInternalCall();
        return -1;
    }
    if (n == PY_SSIZE_T_MAX) {
        PyErr_SetString(PyExc_OverflowError,
            "cannot add more objects to list");
        return -1;
    }

    if (list_resize(self, n+1) == -1)
        return -1;

    if (where < 0) {
        where += n;
        if (where < 0)
            where = 0;
    }
    if (where > n)  // <-- Here the implementation handles indexes > list-len
        where = n;
    items = self->ob_item;
    for (i = n; --i >= where; )
        items[i+1] = items[i];
    Py_INCREF(v);
    items[where] = v;
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...