[Python-ideas] More useful slices

Thomas Kluyver thomas at kluyver.me.uk
Sun Feb 1 19:13:25 CET 2015


On 1 February 2015 at 07:13, Todd <toddrjen at gmail.com> wrote:

> For examples, the following pairs are equivalent:
>
> range(4, 10, 2)
> (4:10:2)
>

I think I remember a proposal somewhere to allow slice notation (defining
slice objects) outside of [] indexing. That would conflict with this idea
of having slice notation define range objects. However, if slice notation
defined slice objects and slice objects were iterable, wouldn't that have
the same benefits?

Iterating over a slice object would work like you were lazily taking that
slice from itertools.count() - i.e. iter(a:b:c) would be equivalent to
islice(count(), a, b, c). This would also mean that (3:) would have a
logical meaning without having to modify range objects to support an
optional upper bound. I don't see any logical way to iterate over a slice
defined with negative numbers (e.g. (-4:)), so presumably iter(-4:) would
raise an exception.

Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150201/4d174ea8/attachment.html>


More information about the Python-ideas mailing list