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

Luciano Ramalho luciano at ramalho.org
Sun Mar 22 08:24:12 CET 2015


On Sun, Mar 22, 2015 at 3:38 AM, pedro santos <probiner at gmail.com> wrote:
> Why can't both 0-base and 1-base indexing exist in the same language, and why can't slicing be customized?

Pedro,

The best answer ever is by one of the most important computer
scientists of the 20th century, Edsger W. Dijkstra -- who was also
Dutch, like Guido van Rossum:

https://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF

The answer is also visually beautiful, as are all Dijkstra's
manuscripts. Some font designer should create a font based on
Dijkstra's handwriting.

Cheers,

Luciano


>
>
>
> If I'm indexing the ruler marks, intervals, boundaries, dots, makes sense to start of at 0; rul=[0,1,2,3,4,5,6] would index every mark on my ruler so that accordingly rul[0]=0, rul[5]=5.
> If I'm indexing the blue circles, natural number quantities, objects, spans, makes sense to start at 1; cir= [1,2,3,4,5] so that cir[1]=1 and cir[5]=5.
>
> Now, a lot of the discussion was to do with slicing coupled with the indexing and I don't totally understand why.
>
> 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?
>
> Even though one could see a ≤ x < b to be more adept to 0-base, and a ≤ x ≤ b to be more adept to 1-base, the way I see it, index base and slicing rules could be somehow independent. And one would index and slice the way it would fit the rationale of the problem or the data, because even slicing a 1-base indexed array with a ≤ x < b, would still produce an expected outcome, as in cir=[1,2,3,4,5] so that cir[1:3]=[1,2] or cir[:3]=[1,2].
> Same thing applying a ≤ x ≤ b to a 0-base indexed array, as in rul[0,1,2,3,4,5] so that rul[:2]=[0,1,2] or rul[0:2]=[0,1,2].
>
> Given that python is an example of human friendly code language, emphasizing readability, wouldn't having 0 and 1 base indexing and custom slicing methods, improve the thought process when writing and reading the code, by fitting them better to specific contexts or data?
> Is there some language that provides both or each language picks only one?
>
> Cheers
>
>
> --
> ________________________________
>
> Pedro Alpiarça dos Santos
> Animator  3DModeler  Illustrator
> >>  http://probiner.x10.mx/
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/




-- 
Luciano Ramalho
|  Author of Fluent Python (O'Reilly, 2015)
|     http://shop.oreilly.com/product/0636920032519.do
|  Professor em: http://python.pro.br
|  Twitter: @ramalhoorg


More information about the Python-ideas mailing list