The Ellipsis object, represented by three consecutive dots (...
), is a built-in feature of Python. This object is a singleton, meaning there is only one instance of it in a Python program. Despite its simplicity, the Ellipsis has several useful applications in different programming scenarios.
In this tutorial, we will explore what the Ellipsis is, how to use it, and its primary use cases.
What is the Ellipsis?
- The Ellipsis is a Python object with no associated methods.
- It is often used to represent something unimplemented or to indicate "ignore this part."
- You can invoke it directly as
...
or by using theEllipsis
keyword.
Use cases of the Ellipsis
Secondary prompt in the Python interpreter
In the Python interpreter, ...
appears as the secondary prompt when creating multiline constructs.
Example
>>> def example():
... print("This is an example.")
The ...
is displayed automatically in the console as you type the body of a function or a similar structure.
Indexing and slicing multidimensional arrays
The Ellipsis simplifies the process of accessing or slicing multidimensional arrays, such as those in NumPy. It helps in accessing ranges of data in high-dimensional structures.
Example
# importing numpy
import numpy as np
# creating a multidimensional (4D) array
array = np.random.rand(2, 2, 2, 2)
# using ellipsis to access the first element in the last dimension
print(array[..., 0]) # equivalent to array[:, :, :, 0]
print(array[Ellipsis, 0])
Output
[[[0.46253663 0.03092289]
[0.72723607 0.75953107]]
[[0.33160093 0.79259324]
[0.76757812 0.21241883]]]
Note
- You cannot use multiple Ellipsis in a single slicing operation, such as
array[..., 1, ...]
. Doing so will result in an error.
Type annotations
The Ellipsis is commonly used in type annotations in Python, especially when working with the typing
module. It represents generic arguments or return types.
Example
from typing import Callable
# function accepting a callable with generic arguments and returning a string
def inject(get_next_item: Callable[..., str]) -> None:
...
# function with a generic return type
def foo(x: ...) -> None:
...
Callable[..., str]
indicates a function that returns a string but can accept arguments of any type.
Replacement for the pass
statement
The Ellipsis can replace the pass
statement in functions or classes that are not yet implemented.
Example
# traditional style
def foo():
pass
# using ellipsis
def bar():
...
Both examples are equivalent, but using ...
can make the code cleaner.
Default argument values
The Ellipsis can be used as a default argument value in functions to distinguish between a missing argument and one explicitly set to None
.
Example
def foo(x=...):
return x
print(foo()) # Output: Ellipsis
print(foo(None)) # Output: None
Error when using multiple Ellipsis
When working with multidimensional structures, remember that only one ...
is allowed per indexing operation. Using more than one will result in an error.
Example
# invalid attempt to use multiple ellipsis
l[..., 1, ...]
Error
IndexError: an index can only have a single ellipsis ('...')
Conclusion
The Ellipsis object (...
) is a versatile feature in Python, useful for:
- Representing incomplete code.
- Simplifying access to multidimensional structures.
- Defining generic types in annotations.
- Replacing
pass
in temporary implementations.
Though often overlooked by beginners, understanding and using the Ellipsis can make working with data easier and improve the readability of your code. Try incorporating it into your projects and see how it simplifies your tasks!
Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source
Article link:http://pybeginners.com/article/using-the-ellipsis-object-in-python/