Open In App

What is Three dots(…) or Ellipsis in Python3

Last Updated : 15 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Ellipsis is a Python Object. It has no Methods. It is a singleton Object i.e. , provides easy access to single instances.

Various Use Cases of Ellipsis (…):

  • Default Secondary Prompt in Python interpreter.
  • Accessing and slicing multidimensional Arrays/NumPy indexing.
  • In type hinting.
  • Used as Pass Statement inside Functions.

Default Secondary Prompt in Python interpreter

Ellipsis notation[…] is used as a default secondary prompt in Python interpreter which is seen during multi-line constructs

Example:

Ellipsis in Python3

Ellipsis in Python3

Accessing and slicing multidimensional Arrays/NumPy indexing

  • Accessing: Giving access to a specified range of elements, just omitting out the serial indices.
  • Slicing: Important use of Ellipsis is in slicing higher-dimensional data structures.

Example:

Suppose, we have a 4-dimensional matrix of order 2x2x2x2. To select all first row elements(in case of row-major structure) in the 4th dimension, we can simply use the ellipsis notation

Python3




# importing numpy
import numpy as np
 
array = np.random.rand(2, 2, 2, 2)
print(array[..., 0])
print(array[Ellipsis, 0])


Output:

[[[0.46253663 0.03092289]
  [0.72723607 0.75953107]]

 [[0.33160093 0.79259324]
  [0.76757812 0.21241883]]]
[[[0.46253663 0.03092289]
  [0.72723607 0.75953107]]

 [[0.33160093 0.79259324]
  [0.76757812 0.21241883]]]

In the above example, [:, :, :, 0], [ … , 0] and [Ellipsis, 0] are all equivalent.

We can not have multiple ellipsis in a single slicing like a[… ,index, …]

In type hinting

Ellipsis is used in specifying type hints using the typing module (e.g. Callable[…, str]). It can serve in either way:

When the argument(s) of the function allows the type: Any

Actually callable takes the arguments: 

Callable "[" parameters_expression, type_expression "]"

(e.g. Callable[…, str]) 

Example:

Python3




from typing import Callable
 
def inject(get_next_item: Callable[..., str]) -> None:
            ...
# Argument type is assumed as type: Any
def foo(x: ...) -> None:
              ...


Using ‘…’ as parameters_expression signifies a function that returns a string without specifying the call signature.

When the return value of the function is of type: Any  

Actually callable returns this way:

Callable "[" parameters_expression, type_expression "]" -> return_type: #body

Example:

Python3




class flow:
   
    # (using "value: Any" to allow arbitrary types)
    def __understand__(self, name: str, value: ...) -> None: ...


Used as Pass Statement inside Functions

Ellipsis is used instead of pass statement inside functions. ‘pass’ replaced by ‘…’ or ‘Ellipsis’. 

Example:

Python3




# style1
def foo():
    pass
# style2
def foo():
    ...
# both the styles are same


Ellipsis can also be used as a default argument value. Especially when you want to distinguish between not passing in value and passing in None.

Example: 

Python3




def foo(x = ...):
    return x
 
print(foo)


Output:

<function foo at 0x7fabdeea6e18>

Error while working with ellipsis

We should always use colons for accessing the other dimensions i.e. ‘ : ‘.

Python3




l[...,1,...]


Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: an index can only have a single ellipsis ('...')


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads