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

Andrew Barnert abarnert at yahoo.com
Sun Mar 22 15:52:07 CET 2015


Your first example is 0-based even though it's closed (L[1:3]=[1,2,3]), which weakens your whole argument for 1-based indexing. and makes me think such a possibility might actually confuse you--which is exactly why we don't have it.

As you say, it may still make sense to have closed ranges and slices alongside half-open, even with 0-based indices. And many newer languages do that, often by using ".." vs. "...", which seems to be pretty readable. But there's no obvious way to add something similar to Python at this point. (Also, I think the fact that ranges and slices are different things with different syntax is a benefit of Python, not a deficit, and it's even harder to design a good solution without losing that benefit...)

Sent from my iPhone

> On Mar 21, 2015, at 11:38 PM, pedro santos <probiner at gmail.com> wrote:
> 
> Hi
> 
> I'm an Art and CG student learning Python and today's exercise was about positions in a tiled room. The fact that I had to check if a position was inside the room and given that in a 1x1 room, 0.0 was considered in and 1.0 was considered out, it kept me thinking about 0-base indexing iterables and slicing.
> 
> Read some articles and discussions, some pros and cons to each 0-base and 1-base, concerns about slicing, etc. But ultimately the question that got stuck in me and didn't found an answer was:
> Why can't both 0-base and 1-base indexing exist in the same language, and why can't slicing be customized?
> 
> 
> 
> 
> 
> 
> 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150322/7de1c6d0/attachment-0001.html>


More information about the Python-ideas mailing list