Expressive Ellipsis in Python

One of the lesser-known things in Python is the ellipsis:

class Flyer:
    def fly(self):

This code works. The ... (aka Ellipsis) is a real object that can be used in code.

Ellipsis is the only instance of the EllipsisType type (similar to how None is the only instance of the NoneType type):

>>> ... is Ellipsis
>>> True
>>> Ellipsis is ...
>>> True

Python core devs mostly use ... to show that a type, method, or function has no implementation — as in the fly() example.

And in type hints:

It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis for the list of arguments in the type hint: Callable[..., ReturnType]

To specify a variable-length tuple of homogeneous type, use literal ellipsis, e.g. Tuple[int, ...]. A plain Tuple is equivalent to Tuple[Any, ...], and in turn to tuple.

# numbers  is a tuple of integer numbers
# summator is a function that accepts arbitrary parameters
#          and returns an integer
def print_sum(numbers: tuple[int, ...], summator: Callable[..., int]):
    total = summator(numbers)

print_sum((1, 2, 3), sum)
# 6

Other developers use Ellipsis for all sorts of bizarre things ツ

Follow @ohmypy on Twitter and subscribe by email to keep up with new posts 🚀