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

Andrew Barnert abarnert at yahoo.com
Thu Oct 31 07:06:50 CET 2013


On Oct 30, 2013, at 15:47, Terry Reedy <tjreedy at udel.edu> wrote:

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

Why would anyone use isinstance(ob, slice)? If you can write "start, stop, step = ob.indices(length) without getting a TypeError, what else do you care about? (Code that _does_ care about the difference probably wouldn't work correctly with any custom slice object anyway.)

If there really is an issue, we could easily add a collections.abc.Slice.

Or... This may be a heretical and/or just stupid idea, but what about reviving the old names __getslice__ and friends (now taking a slice object instead of 2.x's start and stop)? Then the interpreter calls __getslice__ if you use slicing syntax, or if you use indexing syntax with an instance of abc.Slice (or anything but a numbers.Number even?). That way the code is only in one place instead of having to be written in each sequence class.


More information about the Python-ideas mailing list