top of page
Writer's pictureThe Tech Platform

3 Uses of the Ellipsis in Python



Python has a special built-in singleton object called “Ellipsis”. If we input three dots or the word “Ellipsis” in a Python interactive shell, the results are as following:

>>> ...
Ellipsis
>>> Ellipsis
Ellipsis

This simple object seems inconspicuous, but if we use it properly, it can make our lives easier.

This article will introduce the common three scenarios where the ellipsis can be used. After reading, you’ll like this cute singleton object of Python.


1. An Ellipsis Is a Placeholder for Unwritten Code

When designing a new module, we usually define some functions or classes but won’t implement them immediately. Because we only want to determine what we need to write in future and don’t care too much about the implementation details at this early stage. In this scenario, the ellipsis is our best friend:

def write_an_article():
    ...


class Article:
    ...

As shown above, we can just use an ellipsis as a placeholder for a function or a class.


Generally speaking, it’s a good programming practice that we design the needed things at first and implement them later. Because this way can help our mind keep clear about the whole structure and don’t get stuck in details right away.


By the way, the “pass” keyword can also be used as a placeholder in Python:

def write_an_article():
    pass


class Article:
    pass

We can decide which one to use based on personal preference. As far as I am concerned, the ellipsis is cuter.


2. Use an Ellipsis in Numpy To Omit Dimensions

The Numpy is an essential Python library for Data Science. When handling multi-dimension arrays in

Numpy, the ellipsis is helpful.


For example, if we have a 3-d matrix and want to slice it, there are three ways:

import numpy as np

A = np.random.rand(2, 3, 2)
print(A)
# [[[1.13948755e-01 6.75415667e-04]
#   [1.08273680e-01 4.58155559e-01]
#   [5.87369042e-01 9.02622630e-01]]
# 
#  [[6.17789457e-01 9.13353920e-01]
#   [9.53150493e-01 4.70330813e-01]
#   [5.46381962e-01 6.42168707e-01]]]

print(A[:, :, 1])
# [[6.75415667e-04 4.58155559e-01 9.02622630e-01]
#  [9.13353920e-01 4.70330813e-01 6.42168707e-01]]

print(A[..., 1])
# [[6.75415667e-04 4.58155559e-01 9.02622630e-01]
# [9.13353920e-01 4.70330813e-01 6.42168707e-01]]

print(A[Ellipsis, 1])
# [[6.75415667e-04 4.58155559e-01 9.02622630e-01]
#  [9.13353920e-01 4.70330813e-01 6.42168707e-01]]

As shown above, using the three dots is the most economical way to slice a multi-dimension matrix. Because it requires the least typing stuff. Programmers’ time is money, isn’t it?


3. Use the Ellipsis for Type Hinting

The type hinting was a new feature from Python version 3.5. Base on the PEP 484, the ellipsis has special usages within this feature.


On the one hand,

Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example, Tuple[int, ...].

On the other hand,

It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis (three dots) for the list of arguments:
def partial(func: Callable[..., str], *args) -> Callable[..., str]:
    # Body


Conclusion

The ellipsis is an interesting syntax sugar in Python. It’s helpful in some scenarios, and more importantly,



Source: Medium


The Tech Platform

0 comments

Recent Posts

See All

Comments


bottom of page