Questions regarding design decisions in listobject.c

Christian Tismer tismer at tismer.com
Sun Jun 4 17:21:04 EDT 2000


Courageous wrote:
> 
> 1. For slice operations, ilow/ihigh indices which undeflow
> or overflow the list in question are reset to the edges
> of the list. Why was it that PyExc_IndexError wasn't
> thrown instead?

Slice operations are defined to never fail.

> 2. In slice expressions of the form s[i:j], what was the
> design decision that lead to s[i:i] always returning an
> empty list and s[i:i+1] selecting one item. At first
> glance, it would appear that s[i:i] is the obvious and
> intuitive correct expression to return just one item on
> a slice, where s[i:i+1] ought to select two items and so
> forth. Was there some particular situation which arose
> which required this slightly counterintuitive idiom?

All Python indexing is like [a, b), so you have a right-open
interval. It may look a little counter-intuitive at first
glance, but finally it is not at all.
The number of elements covered in a slice operation is
always the difference between the bounds for normal cases.
Normal cases means positive indexes which are inside the
list's size, of course.
Becoming used to it takes a bit of re-thinking, but finally
I believe it greatly reduces the number of off-by-one errors.

advocating-ly - y'rs - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com




More information about the Python-list mailing list