Try typing ...
in the Python3 REPL.
Python3
>>> ...
Ellipsis
This is an Ellipsis object.
A singleton object represented by ʻEllipsis or
... `.
In the Official Reference, "Mainly extended slice syntax and user-defined container data It only says "a special value used in a type" and does not clearly define its use.
The English ellipsis means "omitted", so it can be used when you want to omit processing or arguments.
None
None
means" no value exists ", while ʻEllipsis` means" some value exists but is omitted ".
From the fact that the return value of bool (Ellipsis)
is True
, it can be seen that ʻEllipsis has a more positive meaning than
None`.
Python3
>>> bool(None)
False
>>> bool(Ellipsis)
True
pass
pass
is a statement, but...
is an expression.
That is, ...
can be given to a function as an argument.
Python3
>>> str(Ellipsis)
'Ellipsis'
>>> str(pass)
File "<stdin>", line 1
str(pass)
^
SyntaxError: invalid syntax
In the first place, Python is a language called the original "executable pseudo code".
This means that Python code is not just an "executable program", but an "expression of processing and algorithms that both humans and computers can understand."
In pseudo code, abbreviated parts such as processing are sometimes written as ..., but it can be said that Python3 enables more abstract expression by recognizing this as a value representing "omission".
A more specific example.
NumPy allows you to use ...
in slice notation for multidimensional arrays.
In this case, ...
indicates that the index of the unspecified dimension is arbitrary.
Python3-NumPy
>>> import numpy
>>> a = numpy.array([[1, 2],
... [3, 4]]) #2x2 multidimensional array
>>> a[0, ...] #An array of elements from any column in row 0
array([1, 2])
>>> a[1, ...] #An array of elements from any column in the first row
array([3, 4])
>>> a[..., 0] #An array of elements in column 0 of any row
array([1, 3])
>>> a[..., 1] #An array of elements in the first column of any row
array([2, 4])
>>> a[...] #An array of elements from any column in any row
array([[1, 2],
[3, 4]])
You might think that you can do the same with :
in slice notation.
Python3-NumPy
>>> a[0, :]
array([1, 2])
However, the behavior of :
and ...
is different for arrays of three or more dimensions.
:
Indicates that one particular dimension is arbitrary, while ...
indicates that the index is arbitrary for all dimensions not specified.
>>> b = numpy.array([[[1, 2],
... [3, 4]],
... [[5, 6],
... [7, 8]]]) #2x2x2 multidimensional array
>>> b[..., 0] #1st,2D is arbitrary, 3D index is 0---(1)
array([[1, 3],
[5, 7]])
>>> b[:, :, 0] # (1)Synonymous with
array([[1, 3],
[5, 7]])
>>> b[:, 0] #1st dimension is arbitrary, 2nd dimension index is 0, 3rd dimension index is arbitrary---(2)
array([[1, 2],
[5, 6]])
>>> b[:, 0, :] # (2)Synonymous with
array([[1, 2],
[5, 6]])
Recommended Posts