This came up in another question recently. I'll elaborate on my answer from there:
Ellipsis is an object that can appear in slice notation. For example:
myList[1:2, ..., 0]
Its interpretation is purely up to whatever implements the __getitem__
function and sees Ellipsis
objects there, but its main (and intended) use is in the numpy third-party library, which adds a multidimensional array type. Since there are more than one dimensions, slicing becomes more complex than just a start and stop index; it is useful to be able to slice in multiple dimensions as well. E.g., given a 4 × 4 array, the top left area would be defined by the slice [:2, :2]
:
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
>>> a[:2, :2] # top left
array([[1, 2],
[5, 6]])
Extending this further, Ellipsis is used here to indicate a placeholder for the rest of the array dimensions not specified. Think of it as indicating the full slice [:]
for all the dimensions in the gap it is placed, so for a 3d array, a[..., 0]
is the same as a[:, :, 0]
and for 4d a[:, :, :, 0]
, similarly, a[0, ..., 0]
is a[0, :, :, 0]
(with however many colons in the middle make up the full number of dimensions in the array).
Interestingly, in python3, the Ellipsis literal (...
) is usable outside the slice syntax, so you can actually write:
>>> ...
Ellipsis
EDIT: Ellipsis is also used in the standard library typing
module: e.g. Callable[..., int]
to indicate a callable that returns an int
without specifying the signature, or tuple[str, ...]
to indicate a variable-length homogeneous tuple of strings.
x=[];x.append(x);print(x)
, to see how it handled stringifying cyclical objects. It returned[[...]]
. I thought "I wonder what happens if I type in[[...]]
? My guess was it would throw a syntax error. Instead, it returned[[Ellipsis]]
. Python is so weird. The Google search that ensued brought me to this page. – World...
in a recursive repr is just a placeholder and has no relation toEllipsis
– Midrib...
or the built-in name Ellipsis. Its truth value is true. – Icelandic