Python Forum

Full Version: how is Ellipsis or ... used?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how is Ellipsis or ... used in Python code? has anyone here had a need to use it? has anyone here ever seen it used in code not in a comment?

...
It is used in rumpy array slicing and also in the typing module. However it was added to Python because some folks thought that it would be cute to be able to write
def spam(): ...
and Guido agreed
(Oct-01-2022, 07:24 PM)Gribouillis Wrote: [ -> ]rumpy
rumpy?
it was just a typo it's numpy
this one is the use case that looks cute to me.
Useful for numpy if you have a multidimensional array and want to have a placeholder to address a range of dimensions. The ... consume as many elements, as dimensions left. The comma in the brackets addresses the next dimension.

import numpy as np


radar_data = np.ones([3, 256, 256, 2])
# 3 Sensors, 256 chirps, fft 256 values, i and q data
radar_iq = 1j * radar_data[..., 0] + radar_data[..., 1]
# 3 Sensors, 256 chirps, fft 256 values complex 128
The use for typhints came afterwards.
and numpy is the (initially only) implementation recognizing ... (e.g. nothing in the interpreter does so other than seeing that it is a different type than expected)?

can i use ... for something else (not saying what for) that does not involve numpy?
(Oct-03-2022, 10:25 PM)Skaperen Wrote: [ -> ]can i use ... for something else
Of course, use it for something else. Python is excellent for experimenting things.
(Oct-03-2022, 10:25 PM)Skaperen Wrote: [ -> ]can i use ... for something else (not saying what for)

Yes. For example, you can use this object as a sentinel. The ... which is Ellipsis is a singleton like True, False and None. So, they exist only once per process. The difference to None is, that bool(Ellipsis) returns True.
so i should compare with "is" like i do for True, False, None.