Ellipsis Object
Programming
Coding Terms
Object-Oriented Programming
Python

What does the Ellipsis object do?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

The Ellipsis object, often represented by three consecutive dots ..., is a special object in Python. It is commonly utilized in the realm of advanced Python functionalities, including but not limited to multidimensional array slicing and customized container object handling.

Understanding Ellipsis in Python

Ellipsis is a singleton pre-defined object in Python, similar to None and True. It’s an instance of the _EllipsisType and is a unique type. You can reference it directly in your code as an Ellipsis or by its shorthand notation ....

Common Use Cases

1. Advanced Slicing of Multidimensional Arrays

One of the primary uses of the Ellipsis object is in the slicing of multi-dimensional arrays, particularly when using libraries such as NumPy. It allows the selection of all items in dimensions that haven’t been explicitly mentioned in a slicing operation.

Example:

python
1import numpy as np
2
3# Create a 3D numpy array
4arr = np.array([[[1, 2], [3, 4]], 
5                [[5, 6], [7, 8]]])
6
7# Regular Python slicing
8print(arr[1, ...])
9# Output: [[5 6]
10#          [7 8]]
11
12# Equivalent to
13print(arr[1, :, :])
14# Output: [[5 6]
15#          [7 8]]

This shows how ... is used to represent complete slices in omitted dimensions.

2. Custom Containers and Classes

In custom-made classes or containers, Ellipsis can be used for specific yet customized handling, such as marking unimplemented parts or as a placeholder.

Example:

python
1class CustomClass:
2    def __getitem__(self, item):
3        if item is Ellipsis:
4            return "Returning everything"
5        return "Returning item"
6
7obj = CustomClass()
8print(obj[...])
9# Output: Returning everything

Comparison to Other Special Objects

While Ellipsis shares some similarities with None and NotImplemented, it serves a distinct purpose. None is used to denote the absence of a value or a null reference, while NotImplemented indicates that an operation between types is not implemented.

Table: Summary of Ellipsis Object Use

FeatureDescription
DefinitionA singleton Python object used in specific contexts like array slicing.
RepresentationCan be referenced by Ellipsis or the shorthand ....
TypeIt is the only instance of the _EllipsisType.
UsagePrimarily used in slices of multidimensional arrays and in user-defined classes for customized behavior.
ComparisonUnlike None or NotImplemented, which signify non-existence or unimplemented operations, Ellipsis represents an intentional omission in sequences or structures.

Python Internals and Ellipsis

Within Python’s C source code, Ellipsis is implemented as Py_Ellipsis, highlighting its integral role at the language’s core level.

Other Considerations

While using Ellipsis is exceedingly rare in typical Python application development, its presence is indispensable for complex, high-dimensional data manipulation tasks, where succinctness and clarity are paramount.

In summary, the Ellipsis object is a specialized, versatile tool tailored primarily for situations requiring the explicit handling of omitted parts in data structures or the provision for complex slicing mechanisms. Its understanding is crucial for developers looking to harness Python's full capabilities in data-intensive applications.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.