[Python-Dev] List insert at index that is well out of range - behaves like append

Guido van Rossum guido at python.org
Tue Sep 16 00:54:57 CEST 2014


This functionality has existed since the earliest days of Python, and even
if we all agreed it was wrong we couldn't change it -- it would just break
too much existing code. I can't quite remember why I did it that way but it
was definitely a conscious choice; probably some symmetry or edge case.
(Note that it works this way at the other end too -- a.insert(-100, x) will
insert x at the beginning of a, if a has fewer than 100 elements.)

On Mon, Sep 15, 2014 at 3:29 PM, Mark Shannon <mark at hotpy.org> wrote:

>
>
> On 15/09/14 12:31, Tal Einat wrote:
>
>> On Mon, Sep 15, 2014 at 6:18 AM, Harish Tech <technews.full at gmail.com>
>> wrote:
>>
>>> 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 dynamic 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 (and
>>> this is
>>> how I expected to behave and it behaved as per my expectation) at least
>>> to
>>> me . So what I felt was either it should throw exception or do the way
>>> ruby
>>> handles it .
>>>
>>>
>>> Is ruby way of handling not the obvious way ?
>>>
>>> I even raised it in stackoverflow
>>> http://stackoverflow.com/questions/25840177/list-
>>> insert-at-index-that-is-well-out-of-range-behaves-like-append
>>>
>>> and got some responses .
>>>
>>
>> Hello Harish,
>>
>> The appropriate place to ask questions like this is python-list [1],
>> or perhaps Stack Overflow.
>>
>
> I think this is an OK forum for this question.
> If someone isn't sure if something is a bug or not, then why not ask here
> before reporting it on the bug tracker?
>
> This does seem strange behaviour, and the documentation for list.insert
> gives no clue as to why this behaviour was chosen.
>
> Cheers,
> Mark.
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> guido%40python.org
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140915/7b5430a0/attachment-0001.html>


More information about the Python-Dev mailing list