Table of Contents

Python ellipsis

Python ellipsis (...) is a literal value representing an undefined, incomplete, or indeterminate result. It's most commonly used as a placeholder in function stubs, but it's a real object with special semantics in slicing, type hints, and protocol definitions.

Use ellipsis as a sentinel value, placeholder in development, or to represent “all remaining dimensions” in NumPy arrays.

Example

This example shows ellipsis in various contexts.

# run: python3 ellipsis.py
# description: ellipsis as placeholder and sentinel
 
# Ellipsis as placeholder in stubs
def function_todo():
    """Not implemented yet."""
    ...
 
# Check that it's a real object
print(f"Ellipsis value: {Ellipsis}")
print(f"... is Ellipsis: {... is Ellipsis}")
print(f"Type: {type(...)}")
 
# Ellipsis in slicing (NumPy use case)
# a[...] means "all dimensions"
# a[..., 0] means "all dimensions, then index 0 on last"
 
class Matrix:
    def __init__(self, data):
        self.data = data
 
    def __getitem__(self, key):
        if key is ...:
            return self.data
        return self.data[key]
 
m = Matrix([[1, 2], [3, 4]])
print(f"\nMatrix with ellipsis: {m[...]}")
 
# Ellipsis in type hints
def process(value: int | str | ... = ...) -> int | ...:
    """Function that can return int or undefined."""
    if isinstance(value, int):
        return value
    return ...
 
print(f"Process result: {process(42)}")
print(f"Process undefined: {process('skip')}")
 
# Ellipsis as sentinel for "do nothing"
def maybe_process(data, transform=...):
    if transform is ...:
        return data
    return transform(data)
 
print(f"No transform: {maybe_process([1, 2, 3])}")
print(f"With transform: {maybe_process([1, 2, 3], lambda x: [i*2 for i in x])}")

Common patterns

Placeholder in stubs:

In slicing (NumPy arrays, pandas):

In type hints:

As a sentinel value:

Representing incomplete results:

Comparison: