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

Terry Reedy tjreedy at udel.edu
Wed Oct 30 23:47:45 CET 2013


On 10/30/2013 11:25 AM, Nick Coghlan wrote:
>
> On 31 Oct 2013 00:43, "Oscar Benjamin"

>  > 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.

I though of using a magic symbol, $, for that -- a[$-n]. But aside from
the issue of using one of the 2 remaining unused ascii symbols for
something that can already be done, it would not work in a slice call.

> 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?"

Indeed you did ;-)
 From the fourth message of http://bugs.python.org/issue17279:

"From the current [2013 Feb] python-ideas 'range' thread:
Me: Would it be correct to say (now) that all 4 are intentional 
omissions? and not merely oversights?
Nick: Yes, I think so. People will have to be *real* convincing to 
explain a case where composition isn't a more appropriate solution."

I think one point is that if seq.__getitem__(ob) uses 'if isinstance(ob, 
slice):' instead of 'if type(ob) is slice:', subclass instances will 
work whereas wrapper instances would not. I would make range 
subclassable at the same time.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list