Why Python does *SLICING* the way it does??

Mike Meyer mwm at mired.org
Wed Apr 20 23:00:26 EDT 2005


Antoon Pardon <apardon at forel.vub.ac.be> writes:

> Op 2005-04-20, Torsten Bronger schreef <bronger at physik.rwth-aachen.de>:
>> Hallöchen!
>>
>> nde at no.spam.org (Nick Efford) writes:
>>
>>> seberino at spawar.navy.mil <seberino at spawar.navy.mil> wrote:
>>>> Many people I know ask why Python does slicing the way it does.....
>>>
>>>> Can anyone /please/ give me a good defense/justification???
>>>
>>>> I'm referring to why mystring[:4] gives me elements 0, 1, 2 and 3
>>>> but *NOT* mystring[4] (5th element).
>>>
>>> mystring[:4] can be read as "the first four characters of
>>> mystring".  If it included mystring[4], you'd have to read it as
>>> "the first five characters of mystring", which wouldn't match the
>>> appearance of '4' in the slice.
>>>
>>> [...]
>>>
>>> It all makes perfect sense when you look at it this way!
>>
>> Well, also in my experience every variant has its warts.  You'll
>> never avoid the "i+1" or "i-1" expressions in your indices or loops
>> (or your mind ;).
>>
>> It's interesting to muse about a language that starts at "1" for all
>> arrays and strings, as some more or less obsolete languages do.  I
>> think this is more intuitive, since most people (including
>> mathematicians) start counting at "1".  The reason for starting at
>> "0" is easier memory address calculation, so nothing for really high
>> level languages.
>
> Personnaly I would like to have the choice. Sometimes I prefer to
> start at 0, sometimes at 1 and other times at -13 or +7.

Some HLLs have had arrays that let you declare the first index as well
as the size for decades now. Algol-68, for instance. Modern language
still offer such features, but none seem to have been very popular.

Of course, in any reasonable OO language, you can roll your own. So
the question comes down to whether or not these get into the standard
library.

   <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list