Syntax for making slices

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.

Also note that:

my_list[my_slice] = my_other_list

is already supported

What’s wrong with slice(1, 2, 3)?

1 Like

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.

7 Likes

Now that you say that, this conversation rang a bell…I don’t think this is going to happen but it is a common idea.

(also I’m going to remove typing from this, this isn’t a typing topic)

Fact, it can definitely be made more convenient. It is just very verbose and damages readability in certain cases. E.g.:

def foo(bar=slice(1, 2, 2)):
    pass

Also, numpy has a util for it: numpy.s_[0:1]

Another fact. Very unlikely that such syntax change will be deemed as worthwhile for the benefits it offers.

Adding utility such as numpy did in some convenient place might be worth it. To me it would be useful in places where numpy is not a dependency.

Of course, one can always make:

s_ = type('slc', (), {'__getitem__': lambda s, idx: idx})()
s_[1:2:2]

but it could be more convenient than that.

+1 on the idea of exploring more convenient solutions.
-1 on changing syntax for this.

2 Likes

There is a more suitable and intuitive syntax, perhaps.

Edit:
And I’ve seen people mention in here class_getitem, I think that it can be a nice addition, and can be used also in range.

This is no briefer than slice(1,2,3) though.

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.

1 Like

There’s also this one:

I thought I had also proposed it myself, but it seems that I merely posted a few times in that thread.