Hey In one of my projects I'm stuck in an environment where there are multiple co-existing coding environments, one of which has a convention of using 0 based indexing and the other using 1 based indexing. All i can say is that is is a VERY (i cant possibly emphasize this enough) bad idea. it is an unending source of confusion and off by 1 bugs. It is like the line endings issue all over again ... CR or LF and then you end up with CR, LF and CR LF, choosing 1 is definitely better than choosing both. Python got this right, please do not inflict this upon the python community! I got real life experience with this and i'm telling you this is a disaster waiting to happen, please don't.. On 22 March 2015 at 09:24, Luciano Ramalho <luciano@ramalho.org> wrote:
On Sun, Mar 22, 2015 at 3:38 AM, pedro santos <probiner@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
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
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. 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
_______________________________________________ Python-ideas mailing list Python-ideas@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 _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/