19.03.21 01:44, Cameron Simpson пише:
> 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.

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.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/3NSZPARJHEE2ZOKNB2ENAN55GXGIIXL3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to