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

Antoon Pardon apardon at forel.vub.ac.be
Thu Apr 21 03:01:02 EDT 2005


Op 2005-04-20, Peter Hansen schreef <peter at engcorp.com>:
> Terry Hancock wrote:
>> However, I used to make "off by one" errors all the time in both C and Fortran,
>> whereas I hardly ever make them in Python.
>
> This should probably be the overriding concern in this
> case.
>
> I can't remember the last time I made an off-by-one error
> in Python (or, really, whether I ever have), whereas I
> can't remember the last C program I wrote which didn't have
> one.

I do so frequently.

I often have to process files where each line is a record,
each record having a number of fields seperated with a
delimiter.

So the idiom for treating such files has become

  for line in the_file:
    lst = line.split(delimiter)

The problem is that the fields in lst are associated
with a number that is off by one as they are normally
counted. If I go and ask my colleague which field
contains some specific data and he answers:
the 5th, I have to remind my self I want lst[4]

This is often a cause for errors.

-- 
Antoon Pardon



More information about the Python-list mailing list