[Python-ideas] Where did we go wrong with negative stride?

Nick Coghlan ncoghlan at gmail.com
Wed Oct 30 16:25:25 CET 2013


On 31 Oct 2013 00:43, "Oscar Benjamin" <oscar.j.benjamin at gmail.com> wrote:
>
> On 30 October 2013 13:45, Nick Coghlan <ncoghlan at gmail.com> wrote:
> > On 30 October 2013 20:22, Nick Coghlan <ncoghlan at gmail.com> wrote:
> >>
> >> That means that until people have fully explored exactly the semantics
> >> they want in terms of the existing object model, just as I did for
> >> rslice(), then there are *zero* grounds to be discussing syntax
> >> changes that provide those new semantics.
> >
> > Hmm, looks like my rslice testing was broken. Anyway, I created an
> > enhanced version people using the "End - idx" notation from the end
> > that actually passes more systematic testing:
> >
> > https://bitbucket.org/ncoghlan/misc/src/default/rslice.py?at=default
>
> It took me a while to get to that link. I think bitbucket may be
> having server problems.
>
> >>>> from rslice import rslice, betterslice, End
> >>>> betterslice(-4, 5)
> > slice(0, 5, 1)
> >>>> betterslice(End-4, 5)
> > slice(-4, 5, 1)
> >>>> rslice(-4, 5).as_slice(10)
> > slice(4, -11, -1)
> >>>> rslice(End-4, 5).as_slice(10)
> > slice(4, -5, -1)
>
> I like the idea of a magic End object. I would be happy to see
> negative indexing deprecated in favour of that. For this to really be
> useful though it needs to apply to ordinary indexing as well as
> slicing. If it also becomes an error to use negative indices then you
> get proper bounds checking as well as an explicit way to show when
> you're indexing from the end which is a substantial improvement.

That's much harder to do in a backwards compatible way without introducing
both the index() and rindex() types Ron (I think?) suggested (the End
object in my proof-of-concept is a stripped down rindex type), and even
then it's hard to provide both clamping for slices and an index error for
out of bounds item lookup. They both also have the problem that __index__
isn't allowed to return None.

Regardless, the main thing I got out of writing that proof of concept is
that I'd now be +1 on a patch to make it possible and practical to inherit
from slice objects to override their construction and their indices()
method. Previously I would have asked "What's the point?"

Cheers,
Nick.

>
>
> Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131031/bf46df4f/attachment-0001.html>


More information about the Python-ideas mailing list