[Python-ideas] 0-base and 1-base indexed iterables? Custom slicing rules?

Chris Angelico rosuav at gmail.com
Sun Mar 22 15:30:12 CET 2015


On Sun, Mar 22, 2015 at 5:38 PM, pedro santos <probiner at gmail.com> wrote:
>
> a ≤ x < b is not so intuitive when dealing with objects ("I want balls 1 up to the the one before 3"), so on one side, you put the finger on what you want and on the other, on what you don't want. But this method does have the neat property of producing neighbor selections that border perfectly, as in [:a][a:b][b:c]. Although in inverse order(-1), the results can be unexpected as it returns values off-by-one from its counterpart like; L=[0,1,2,3,4,5] so that L[1:3]=[1,2] and L[3:1:-1]=[3:2]. So it's consistent with the rule a ≤ x < b, grabbing the lower limit item, but it can feel strange by not producing the same selection with inverse order.
>
> a ≤ x ≤ b is a natural way to select objets ("I want the balls 1 up to 3"), so you're putting the finger on the things you want. If you inverse the order(-1) it's still very easy to grasp what are you picking because whatever you select it's included like: L=[0,1,2,3,4,5] so that L[1:3]=[1,2,3] and L[3:1:-1]=[3,2,1]. Problems seem to arrive though, when trying to do neighbor selections, where one would have to do [:a][a+1:b][b+1:c] to have the border perfectly. That terrible?
>

To add to what other people have said in support of Python's indexing
style, I'd like to point out one situation where fully-closed ranges
are used: Scripture references. Have a look at this page:

https://www.biblegateway.com/passage/?search=Mark+3&version=NIV

This is one chapter from the Bible. Passages within that chapter can
be identified by starting and ending verse, inclusive at both ends.
So, for instance, the paragraph under the heading "Jesus Appoints the
Twelve" would be Mark 3:13-19. Note how the end point is actually one
less than the number that starts the next passage. Python identifies
the boundaries between elements, which corresponds perfectly to a
numbering system such as this; noting the passage as "Mark 3:13-20"
would mean you start reading at the indicator for verse 13, and stop
reading as soon as you reach the indicator for verse 20, which makes a
lot more sense.

Obviously it's way WAY too hard to change something as extensively
used as Bible references - the backward incompatibility concern is
just too strong - but this may give some indication of why it's more
sensible to work with half-open ranges. A half-open range also gives a
convenient notation for "to the end of the chapter": instead of "Mark
3:20-3:35", it would simply be "Mark 3:20-4:1". (Chapters and verses
are numbered from one, not zero, but in an ideal world, this would
also change, so it would be "3:19-4:0".)

ChrisA


More information about the Python-ideas mailing list