On Thu, Aug 12, 2021 at 5:16 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, Aug 12, 2021 at 09:57:06AM -0400, Ricky Teachey wrote:

> This got me thinking just now: allowing ellipses instead of None for the
> first two arguments of the slice() constructor might be a neat idea.

Like this?

>>> obj = slice(..., ..., 5)
>>> print(obj)
slice(Ellipsis, Ellipsis, 5)

The arguments to slice can be anything you like.

>>> slice({'a': object()}, int, ('spam', 'eggs'))
slice({'a': <object object at 0x7f0067ed9420>}, <class 'int'>, ('spam', 'eggs'))

and the interpretation is entirely up to the class:

>>> class C:
...     def __getitem__(self, obj):
...             return obj
...
>>> C()["Hello world!":[]:2.5j]
slice('Hello world!', [], 2.5j)

Aside from the use of a literal `...` for Ellipsis, which is a lot more
recent, this behaviour goes back to Python 1.5 or older. Slices have
always been liberal about what they accept.


--
Steve

Oh man thanks for pointing that out... and of course numpy and pandas and many other libraries already use ellipses for various things in slices.

and of course native python sequences already have far better syntactic sugar than ellipses: 

[][3:]
[][::-1]

etc etc.

So given that, why would we ever want to use ellipses?!?!


---
Ricky.

"I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler