Design for slices

Alex Martelli aleaxit at yahoo.com
Wed Jan 31 17:19:57 EST 2001


"Gustaf Liljegren" <gustafl at algonet.se> wrote in message
news:959uue$k02$1 at cubacola.tninet.se...
> I'm learning Python and have a question about defining ranges/slices in
> strings. I can't find any defence for what looks like a poor design, so I
> have to ask. Why is the first character in the string x defined as x[0]
and
> not x[1]? This looks just like the typical geek thinking that I'm trying
to
> avoid with Python.

I've seen geeks enough around here (including yours truly) that I
doubt you'll "avoid geek thinking" with Python.  It's just superior
geekhood, not non-geekdom...

Indexing sequences from zero (all sequences are indexed with
the same principles -- it would be a major hassle if they weren't)
is just too practical.  "The first N items of X" are X[:N], just as the
*last* N are X[-N:] -- how much handier and more symmetrical
can you get?  If you ever find yourself mapping richer structures
inside a linear sequence you'll truly bless zero-indexing -- back
in Fortran IV times, when its arrays were indexed from one, I
recall I was all the time subtracting 1 here and having to add it
back over there during index calculations (of course, the horrors
of the upper-bound-*included* idiom were partly responsible --
the current pervasiveness of upper-bound-*excluded* is just as
important as zero-based indices to help eliminating darned
off-by-one errors)...


Alex






More information about the Python-list mailing list