About Python3 ... (Ellipsis object)

What is an Ellipsis object?

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.

Difference from None and pass

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

Where to use

Executable pseudo code

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".

Slicing a multidimensional array in NumPy

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]])

reference

Recommended Posts

About Python3 ... (Ellipsis object)
About python comprehension
Python built-in object
About Python tqdm.
About python yield
Python built-in object
About python inheritance
About python, range ()
About python decorators
About python reference
About Python decorators
[Python] About multi-process
Python Basic Memorandum Part 3-About Object Orientation-
About Python for loops
Summary about Python scraping
About function arguments (python)
[Python] Memo about functions
Summary about Python3 + OpenCV3
About Python, for ~ (range)
About Python3 character code
[Python] Memo about errors
About Python development environment
Python: About function arguments
Object oriented in python
Python, about exception handling
About Python Pyramid traversal
[Python] Chapter 01-01 About Python (First Python)
[Python] About standard input
About __all__ in python
[Python] Find out about pip
About Fabric's support for Python 3
About python objects and classes
About Python variables and objects
[python] Value of function object (?)
About the Python module venv
Null object comparison in Python
Think about architecture in python
About python beginner's memorandum function
About the ease of Python
About various encodings of Python 3
About Python, len () and randint ()
About Perl, Python, PHP, Ruby
About Python datetime and timezone
A memorandum about correlation [Python]
A memorandum about Python mock
About the features of Python
About "for _ in range ():" in python
About Python and os operations
Python # About reference and copy
About Python sort () and reverse ()
A note about [python] __debug__
Prolog Object Orientation for Python Programmers
Python Note: About comparison using is
[Ubuntu] [Python] Object tracking using dlib
About installing Pwntools and Python2 series
Touch a Python object from Elixir
Python
Python: A Note About Classes 1 "Abstract"
[Python] Let's write briefly about comprehensions
About python dict and sorted functions
About dtypes in Python and Cython