
On Sat, Feb 29, 2020 at 4:37 AM Alex Hall alex.mojaki@gmail.com wrote:
It seems like most of this would be very easy to implement yourself with the exact semantics that you prefer and find most intuitive, while other people might have different expectations.
I have to agree here. You are proposing that a slice object be treated as a general purpose interval, but that is not, in fact what they are. This is made clear by: " Presumably, these operations would raise exceptions when used with slices that have step values other than None."
and also: "whereas a slice represents a possibly continuous range of any kind of value to which magnitude is applicable." well, sort of. Given the implementation and duck typing, I suppose that's true. But in fact, slices were designed for, and are (at least mostly) used to, well, slice sequences, which are always integer indexes, and hav semantics specific to that use case:
In [5]: s = slice(2,-2)
In [6]: s.indices(10)
Out[6]: (2, 8, 1)
and what if you do use non-integers?
In [7]: s = slice(2.2,-2.1)
In [8]: s
Out[8]: slice(2.2, -2.1, None)
In [9]: s.indices(10)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-5c4613d5ef69> in <module> ----> 1 s.indices(10)
TypeError: slice indices must be integers or None or have an __index__ method
In short: slices are for, well, slicing, they are no more general intervals than range objects :-)
-CHB