Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!newsfeed.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!82.197.223.103.MISMATCH!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'insert': 0.05; 'assignment': 0.07; '[1,': 0.09; 'append': 0.09; 'indexes': 0.09; 'meaningful': 0.09; 'throws': 0.09; 'python': 0.11; 'bug': 0.12; 'attaching': 0.16; 'behave': 0.16; 'indexerror:': 0.16; 'informing': 0.16; 'skip:j 30': 0.16; 'subject: \n ': 0.16; 'subject:too': 0.16; 'throw': 0.16; 'unexpected': 0.16; 'demonstrate': 0.16; 'exception': 0.16; 'index': 0.16; 'trying': 0.19; 'written': 0.21; 'handles': 0.22; 'error': 0.23; 'lets': 0.24; 'subject:like': 0.24; 'java': 0.24; 'non': 0.24; 'question': 0.24; 'source': 0.25; '>': 0.26; 'handling': 0.26; 'least': 0.26; 'values': 0.27; 'idea': 0.28; 'errors': 0.30; 'originally': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'raised': 0.31; 'subject:that': 0.31; 'handled': 0.32; 'languages': 0.32; 'beginning': 0.33; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'subject:List': 0.36; 'doing': 0.36; 'thanks': 0.36; 'should': 0.36; 'list': 0.37; 'clear': 0.37; 'expected': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'range': 0.61; 'making': 0.63; 'personal': 0.63; 'such': 0.63; 'places': 0.64; 'more': 0.64; 'chance': 0.65; 'situation': 0.65; 'here': 0.66; 'between': 0.67; '100': 0.79; 'silently': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=01asVV/Sw9OR7AugcV0nBYxocN5awrmwBdw0brIyqhs=; b=t9qsL4+bKIx9eVrVgQRkUq7c+OZ13eh/WOH+W7HPhi80UUaiazPqlgeIwymE1a4LPU u6bhNpRubz4yYZssd0HIDGjCurH/1nrQML5bLwxsTqD69M5ikhLOwbhNhhMGKTra3Lmg Rey5d7z0wgIQrH3Jp/eqUZZwDvoUvVznHyjQC0jhrJ3pp/lxd7T4tyvmU17vdawqwUkn CXs44kyniuW+IP0z1IGc0n/+9sAXmX4HOb7DNxWKugAV0btRbZMh5TGK7/LQ8bu+acI+ 1osrz/xWOkT9KaWJudiMH6rvWNZK1B7YLm5Zf4H3Av2B2POB2164nptJOXJ2GrdWymBL Ta4Q== MIME-Version: 1.0 X-Received: by 10.236.24.193 with SMTP id x41mr3075220yhx.66.1410783385561; Mon, 15 Sep 2014 05:16:25 -0700 (PDT) Date: Mon, 15 Sep 2014 17:46:25 +0530 Subject: List insert at index that is well out of range - behaves like append that too SILENTLY From: Harish Tech To: python-list@python.org Content-Type: multipart/alternative; boundary=089e0115f9da8d30250503199a2c X-Mailman-Approved-At: Mon, 15 Sep 2014 14:26:22 +0200 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 222 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1410783983 news.xs4all.nl 2952 [2001:888:2000:d::a6]:51562 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77880 --089e0115f9da8d30250503199a2c Content-Type: text/plain; charset=UTF-8 Hi , Let me demonstrate the problem I encountered : 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 Personal Opinion : Lets see how other languages behave in such a situation : 1. 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 . Here also it was silently handled but the way it fills non existing indexes in between with nil sounded meaningful . 2. Java : When I do such an action in java by using .add(index.value) on may be arraylist or linkedlist I get java.lang.IndexOutOfBoundException Here instead of handling it silently it throws an error . But the python way of handling such a problem by appending to the end sounds more unexpected to me . This in fact flummoxed me in the beginning making me think it could be a bug . Then when I raised it in stackoverflow I got chance to look at source and found that's the way code is written . Question : 1. Any idea Why it has been designed to silently handle this instead of at least informing the user with an exception(as in java) or attaching null values in empty places (as in ruby) ? Thanks Harish --089e0115f9da8d30250503199a2c Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hi ,=

Let= me demonstrate the problem I encountered :

I had a list

=C2=A0a =3D [1, 2, 3]<= /span>

when= I did

a.insert(100, 100)

[1, 2, 3, 100]

as list was originally of size 4 and I was try= ing to insert value at index 100 , it behaved like append instead of throwi= ng any errors as I was trying to insert in an index that did not even exist= ed .


Should it not throw

IndexError: list assignment index out of range

exception as i= t throws when I attempt doing

a[100] =3D 100


Personal Opinion : Lets see how other language= s behave in such a situation :=C2=A0

1. Ruby :

=C2=A0 =C2=A0 > a =3D [1, 2]

=C2=A0 = =C2=A0 > a[100] =3D 100

=C2=A0 =C2=A0 > a

=C2=A0=3D> [1, 2, nil, nil, ni= l, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ni= l, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ni= l, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ni= l, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ni= l, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ni= l, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ni= l, nil, nil, nil, nil, nil, 100]=C2=A0

The way ruby handles this is pretty clea= r and sounds meaningful (and this is how I expected to behave and it behave= d as per my expectation) at least to me . Here also it was silently handled= but the way it fills non existing indexes in between with nil sounded mean= ingful .

2. Java :

When I do such an acti= on in java by using .add(index.value) on may be arraylist or linkedlist I g= et java.lang.IndexOutOfBoundException

Here instead of handling it= silently it throws an error .


But the python way = of handling such a problem by appending to the end sounds more unexpected t= o me . This in fact flummoxed me in the beginning making me think it could = be a bug . Then when I raised it in stackoverflow I got chance to look at s= ource and found that's the way code is written .=C2=A0

Questi= on : 1. Any idea Why it has been designed to silently handle this instead o= f at least informing the user with an exception(as in java) or attaching nu= ll values in empty places (as in ruby) ?


Thank= s

Harish

--089e0115f9da8d30250503199a2c--