Array design question

Terry Reedy tjreedy at udel.edu
Thu May 29 12:09:48 EDT 2003


"Peter Slizik" <peter.slizik at pobox.sk> wrote in message
news:bb4ljk$5pvsi$1 at ID-196014.news.dfncis.de...
> I just don't understand, why Python doesn't allow to exceed array
> boundaries. PHP and Perl would resize the array in this case.
...
> Is there any reason why Python designers chose this concept?

1) The way seqs are intended to be used, indexing out-of-bounds is
often an error which should be caught.  Wanting auto extension is
unsual.

2) Auto-extension requires a choice of how to fill in blanks.  The
appropriate choice depends on the programmer's intention.  It is
generally against Python philosopy for the interpreter make such
guesses.

3) As others have already noted, you can instead use a dict or custom
class.

4) Or you can preallocate a list to the maximum length needed.

5) Auto-extension is dangerous -- like writing a blank check:

> Let's have a simple example. Suppose we have the file in format
> 7 "Text 1"
> 5 "Text 2"
> 3 "Text 3"

> Simple PHP code
>
> while( !eof() ) {
>      line = readline();
>      (number, text) = split(line);
>      array[number] = text;
> }

Suppose the next entry lines are
100000000 "Text 100 mil"
100000000000 "Text 100 bil"

Do you *really* want autoextension, or doen't a sparse implementation,
like {} begin to look better?

6. Auto-write extension opens a least a small can of worms.  To be
consistent, reading out of range should also be allowed, returning the
same default used to pad.  If so, should the extension be virtual (not
change the allocated length) or actual.  In either case, we now have 4
possible list implementations, each of which might be wanted in
certain situations.  We would also need to reconsider the meaning of
out-of-range slices.

Terry J. Reedy






More information about the Python-list mailing list