Add clarification for use of ellipsis

Hey everyone!
Docs say that

This object is commonly used by slicing (see Slicings).

But when I follow a link I can’t find any mentioning of ellipsis there.
Honestly I don’t use it in my day-to-day work and didn’t see any use of it in projects I’m working on. AFAIK library devs can use them for some better syntax (ex. numpy). I played around with it to see if something like this will work in pure python and tried this

x = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

# everything below raises TypeError
print(x[..., 0])

So, my pruposal is to add examples how to use it eighter in HOWTO section or right at their description in built-in types.

1 Like

There is no use of ellipsis for slicing in the stdlib. It is only supported in NumPy and maybe in other third-party libraries.

1 Like

The doc probably shouldn’t say that, or say explicitly that it’s only the case in third-party modules.
Can we cite Numpy in the doc ? (legally yes, of course, I mean as a common doc practice)


For the information, slicing is a syntax sugar for __getitem__, __setitem__, __delitem__ and the builtin slice.

x[..., 0]  # is equivalent to
x.__getitem__((Ellipsis, 0))

x[...][0]  # is equivalent to

x[0:...]  # is equivalent to
x.__getitem__(slice(0, Ellipsis, None))

The actual behavior depends on the implementation of the __getitem__ method. For example, the builtin list type only supports subscriptions with int and slice, but doesn’t support ellipsis or tuple. numpy.ndarray has its own implementation and its own rules which are different from those of list.

In pyparsing, I also use Ellipsis as part of its embedded DSL for defining parsers:

# use of ... for zero-or-more repetition

BEGIN =  = Keyword("BEGIN")
END = Keyword("END")
body_word = Word(alphas)

expr = BEGIN + Group(body_word[...:END]) + END
# equivalent to
# expr = BEGIN + Group(ZeroOrMore(body_word, stop_on=END)) + END

# use of ... as synonym of SkipTo

start_marker = Keyword("START")
end_marker = Keyword("END")
expr = start_marker + ... + end_marker
# equivalent to
# expr = start_marker + SkipTo(end_marker) + end_marker

I’m going to just say: I never knew that Ellipsis was a built-in until i saw this thread. Thanks folks.

I forgot that ellipsis is actually also used in typing. Things like tuple[int, ...] and Callable[..., str]. However, the semantics are different, which is why I forgot about it.

1 Like