Skip to content
Related Articles

Related Articles

What is Three dots(…) or Ellipsis in Python3
  • Last Updated : 05 Sep, 2020

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:



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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

Method 1: 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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Method 2: 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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


  

Used as Pass Statement inside Functions

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

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

<function foo at 0x7fabdeea6e18>

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :