Questions regarding design decisions in listobject.c

Tim Peters tim_one at email.msn.com
Sun Jun 4 17:23:39 EDT 2000


[Courageous]
> 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?

Because it's more convenient more often the way it is.

> 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?

It's highly intuitive after you understand the key point:  in Python,
indices conceptually point *between* elements, not *at* them.  Everything
follows from that.  Once you understand that, it's almost impossible to make
an off-by-1 error, and that len(x[i:j]) = j-i (ignoring pathologies) has a
lot to do with that.  So does that for any k, x[i:j] == x[i:k] + x[k:j]
(again ignoring pathologies).  The "between elements" idea has been
reinvented many times in many languages (e.g., see Icon and Java).  As far
as he knows, Guido thought it up on his own, but I routinely accuse him of
stealing it from Icon <wink>.

whatever-it's-a-major-feature-ly y'rs  - tim






More information about the Python-list mailing list