I think it’d be nice to have a syntax for making slices.
Currently the way to make slices goes as such:
my_slice = slice(1,2,3)
Imagine you have a list, a slice from that list would be:
my_list[1:2:3] = my_other_list
I think it’d make sense to create slices like so:
my_slice = [1:2:3]
It’d not hurt any existing code of lists, since any slice that would get created would need at least 1 ‘:’ character, which lists do not accept.
So in such a case, it’s a slice perhaps.
My preferred syntax for this would actually slice[1:2:3], i.e. slice.__class_getitem__ becomes an identity method. I sometimes define an class S: def __class_getitem__(_, obj): return obj for this purpose.
This option also has the benefit of not requiring any syntax changes, but it is going to make typing people annoyed.
The syntax with : is well known and familiar. It’s more obvious to leave stuff out in slice[::-1] then explicitly writing None and people would have to think about it less.
I suspect the number of use cases for standalone slice objects is quite small. : is prime real estate within the syntax, and is better reserved for more widely-used or anticipated features.
Slicing, as a concept, already has a high mental overhead. Its start/stop/step design has an unfortunate resemblance with that of range and the proposed syntax at first glance seems like a sequence (due to square brackets) rather than a slice. IMHO both range and slice should have been unified long ago, and both are old designs from an era when CPython was the only Python in the world and implementing such magic as start:stop:step syntax and (start, stop, step) overload arguments in C, were viewed as acceptable (meanwhile we still don’t have good overload support in pure Python). Maybe if there would be a Python 4, these things could have been reworked (in slightly backwards-incompatible ways), but I digress.