Fundamentals 6 min read

Unlock Python’s Ellipsis (…) – Powerful Uses Beyond Slicing

Discover how Python’s three-dot ellipsis (…) isn’t just a placeholder but a versatile singleton object used for slicing multidimensional arrays, marking unfinished code, enhancing type hints, and even evaluating to True, with practical examples and insights into its unique behavior.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Unlock Python’s Ellipsis (…) – Powerful Uses Beyond Slicing

How to Use

Using the ellipsis is as simple as typing three dots:

<code>...</code>
“That’s it! In Python, those three dots create an Ellipsis object , a unique singleton you can always rely on. Wherever you use ... , it always refers to the same reliable small object.”

Uses of the Ellipsis

1. Slicing Multidimensional Arrays

If you are using libraries such as NumPy, ... can help you slice complex arrays without writing a lot of code:

<code>import numpy as np

arr = np.arange(27).reshape(3, 3, 3)

print(arr[..., 0])</code>

This trick lets you easily retrieve all remaining dimensions without effort.

2. Placeholder

Sometimes when writing a function you know more code will be added later but it isn’t ready yet. In that case you can use pass or ... , which behave slightly differently:

pass is a simple statement that does nothing. It signals “I haven’t written this part yet,” but because it is a statement you cannot use it as a value.

... (ellipsis) is an expression. It creates a value – the Ellipsis object.

For example, you can write:

<code>def upcoming_feature():
    return ...</code>

Using ... tells everyone “stay tuned, more code is coming!” and it can even be used where a real value is required, keeping the code clear and fun.

3. Enhanced Type Annotations and Stub Files

Ellipsis is also handy when dealing with type hints or writing stub files. If you are unsure how many elements a tuple will contain, you can use ... to indicate an arbitrary length:

<code>from typing import Tuple

def get_numbers() -> Tuple[int, ...]:
    return (1, 2, 3)</code>

In some cases (e.g., FastAPI), ... can denote that a parameter is required, helping make your code clearer.

Singleton

Here’s an interesting fact: ellipsis is a singleton . Every time you write ... , Python uses the same single instance of the Ellipsis object. You can verify it yourself:

<code>print(id(...))
print(id(Ellipsis))</code>

Its type is called EllipsisType (an alias introduced in Python 3.10), emphasizing its unique role. Because ... is a literal, you cannot accidentally change it – it is as constant as None .

Boolean magic : Although ... looks like “nothing,” it actually evaluates to True in a boolean context.

Immutable and sealed : You cannot add new attributes to the Ellipsis object nor inherit from its type. It is a compact, perfectly formed Python design element.

Summary

Python’s three dots may seem like a tiny detail, but they offer delightful uses! Whether you slice arrays, mark future code locations, or write precise type annotations, ... is a tiny tool that brings clarity and power to your code. The next time you see these three dots, remember they are a fully valid part of Python code, not just a placeholder.

PythonPlaceholdersingletonNumPytype hintsEllipsis
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.