shouldn't slices be iterable ?

I know that range(start,end,stride) will produce what I'd want from iter(slice(start,end,stride)), but wouldn't it be reasonable for a slice itself to be iterable? Yes, only one obvious way and all that, but inside eg __getitem__ it seems to me that: if isinstance(index, slice): for i in index: ... do stuff with i ... is the obvious thing to do. Cheers, Cameron Simpson <cs@cskk.id.au>

I whinged:
On 18Mar2021 19:15, Caleb Donovick <donovick@cs.stanford.edu> wrote:
Or perhaps more problematic what happens if only stride is specified?
I'd be happy if the behaviour were the same as range(), yea even to being a concise way to spell range(slice.start,slice.end,slide.stride); who wants to say that if iter(slice) means the same thing? For only stride, ValueError. For positive stride and start > end, empty iteration. Cheers, Cameron Simpson <cs@cskk.id.au>

On Fri, Mar 19, 2021 at 3:18 PM Cameron Simpson <cs@cskk.id.au> wrote:
Have you considered range(*slice.indices(len)) ? You need to provide a length in order for negative or omitted endpoints to make sense, but otherwise it's the same idea. But it's difficult to do that within iteration protocol, as there's no way to provide that all-important length. ChrisA

19.03.21 01:44, Cameron Simpson пише:
It was discussed several times before. It cannot work, because you need to know the length of the sequence to bound limits and to determine the start and end indices if corresponding slice attributes are negative or if stride is negative. If sequence a has 10 elements, a[2:100] should only iterate from 2 to 9, a[-5:-2] should iterate from 5 to 7, and a[::-1] should iterate from 9 to 0. slice.indices() gives you normalized start, end and stride which you can pass to range() for iterating.

I whinged:
On 18Mar2021 19:15, Caleb Donovick <donovick@cs.stanford.edu> wrote:
Or perhaps more problematic what happens if only stride is specified?
I'd be happy if the behaviour were the same as range(), yea even to being a concise way to spell range(slice.start,slice.end,slide.stride); who wants to say that if iter(slice) means the same thing? For only stride, ValueError. For positive stride and start > end, empty iteration. Cheers, Cameron Simpson <cs@cskk.id.au>

On Fri, Mar 19, 2021 at 3:18 PM Cameron Simpson <cs@cskk.id.au> wrote:
Have you considered range(*slice.indices(len)) ? You need to provide a length in order for negative or omitted endpoints to make sense, but otherwise it's the same idea. But it's difficult to do that within iteration protocol, as there's no way to provide that all-important length. ChrisA

19.03.21 01:44, Cameron Simpson пише:
It was discussed several times before. It cannot work, because you need to know the length of the sequence to bound limits and to determine the start and end indices if corresponding slice attributes are negative or if stride is negative. If sequence a has 10 elements, a[2:100] should only iterate from 2 to 9, a[-5:-2] should iterate from 5 to 7, and a[::-1] should iterate from 9 to 0. slice.indices() gives you normalized start, end and stride which you can pass to range() for iterating.
participants (4)
-
Caleb Donovick
-
Cameron Simpson
-
Chris Angelico
-
Serhiy Storchaka