Reputation: 22988
While idly surfing the namespace I noticed an odd looking object called Ellipsis
, it does not seem to be or do anything special, but it's a globally available builtin.
After a search I found that it is used in some obscure variant of the slicing syntax by Numpy and Scipy... but almost nothing else.
Was this object added to the language specifically to support Numpy + Scipy? Does Ellipsis have any generic meaning or use at all?
D:\workspace\numpy>python
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> Ellipsis
Ellipsis
Upvotes: 889
Views: 328762
Reputation: 779
For anyone coming to this answer from working in a codebase with heavy Pydantic use: this is also how Pydantic indicates a field that is required but can be set to None
, which they refer to as "required optional fields". This is why they end up used in FastAPI, too.
Upvotes: 33
Reputation: 119211
This came up in another question recently. I'll elaborate on my answer from there:
Ellipsis is an object that can appear in slice notation. For example:
myList[1:2, ..., 0]
Its interpretation is purely up to whatever implements the __getitem__
function and sees Ellipsis
objects there, but its main (and intended) use is in the numpy third-party library, which adds a multidimensional array type. Since there are more than one dimensions, slicing becomes more complex than just a start and stop index; it is useful to be able to slice in multiple dimensions as well. E.g., given a 4 × 4 array, the top left area would be defined by the slice [:2, :2]
:
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
>>> a[:2, :2] # top left
array([[1, 2],
[5, 6]])
Extending this further, Ellipsis is used here to indicate a placeholder for the rest of the array dimensions not specified. Think of it as indicating the full slice [:]
for all the dimensions in the gap it is placed, so for a 3d array, a[..., 0]
is the same as a[:, :, 0]
and for 4d a[:, :, :, 0]
, similarly, a[0, ..., 0]
is a[0, :, :, 0]
(with however many colons in the middle make up the full number of dimensions in the array).
Interestingly, in python3, the Ellipsis literal (...
) is usable outside the slice syntax, so you can actually write:
>>> ...
Ellipsis
EDIT: Ellipsis is also used in the standard library typing
module: e.g. Callable[..., int]
to indicate a callable that returns an int
without specifying the signature, or Tuple[str, ...]
to indicate a variable-length homogeneous tuple of strings.
Upvotes: 868
Reputation: 382442
__getitem__
minimal ...
example in a custom class
When the magic syntax ...
gets passed to []
in a custom class, __getitem__()
receives a Ellipsis
class object.
The class can then do whatever it wants with this Singleton object.
Example:
class C(object):
def __getitem__(self, k):
return k
# Single argument is passed directly.
assert C()[0] == 0
# Multiple indices generate a tuple.
assert C()[0, 1] == (0, 1)
# Slice notation generates a slice object.
assert C()[1:2:3] == slice(1, 2, 3)
# Empty slice entries become None.
assert C()[:2:] == slice(None, 2, None)
# Ellipsis notation generates the Ellipsis class object.
# Ellipsis is a singleton, so we can compare with `is`.
assert C()[...] is Ellipsis
# Everything mixed up.
assert C()[1, 2:3:4, ..., 6, :7:, ..., 8] == \
(1, slice(2,3,4), Ellipsis, 6, slice(None,7,None), Ellipsis, 8)
The Python built-in list
class chooses to give it the semantic of a range, and any sane usage of it should too of course.
Personally, I'd just stay away from it in my APIs, and create a separate, more explicit method instead.
Tested in Python 3.5.2 and 2.7.12.
Upvotes: 29
Reputation: 95
FastAPI makes use of the Ellipsis for creating required Parameters. https://fastapi.tiangolo.com/tutorial/query-params-str-validations/
Upvotes: 3
Reputation: 301
In typer ...
is used to create required parameters: The Argument
class expects a default value, and if you pass the ...
it will complain if the user does not pass the particular argument.
You could use None
for the same if Ellipsis
was not there, but this would remove the opportunity to express that None
is the default value, in case that made any sense in your program.
Upvotes: 2
Reputation: 95901
In Python 3, you can¹ use the Ellipsis literal ...
as a “nop” placeholder for code that hasn't been written yet:
def will_do_something():
...
This is not magic; any expression can be used instead of ...
, e.g.:
def will_do_something():
1
(Can't use the word “sanctioned”, but I can say that this use was not outrightly rejected by Guido.)
¹ 'can' not in {'must', 'should'}
Upvotes: 392
Reputation: 13324
From the Python documentation:
This object is commonly used by slicing (see Slicings). It supports no special operations. There is exactly one ellipsis object, named Ellipsis (a built-in name).
type(Ellipsis)()
produces the Ellipsis singleton.It is written as
Ellipsis
or...
.
Upvotes: 41
Reputation: 4614
As mentioned by @noɥʇʎԀʎzɐɹƆ and @phoenix - You can indeed use it in stub files. e.g.
class Foo:
bar: Any = ...
def __init__(self, name: str=...) -> None: ...
More information and examples of how to use this ellipsis can be discovered here https://www.python.org/dev/peps/pep-0484/#stub-files
Upvotes: 16
Reputation: 46291
This is equivalent.
l=[..., 1,2,3]
l=[Ellipsis, 1,2,3]
...
is a constant defined inside built-in constants
.
Ellipsis
The same as the ellipsis literal “...”. Special value used mostly in conjunction with extended slicing syntax for user-defined container data types.
Upvotes: 6
Reputation: 3336
Summing up what others have said, as of Python 3, Ellipsis is essentially another singleton constant similar to None
, but without a particular intended use. Existing uses include:
Callable[..., int]
or Tuple[str, ...]
)Possible uses could include:
None
is a valid optionUpvotes: 77
Reputation: 9139
As of Python 3.5 and PEP484, the literal ellipsis is used to denote certain types to a static type checker when using the typing module.
Example 1:
Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, for example
Tuple[int, ...]
Example 2:
It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis (three dots) for the list of arguments:
def partial(func: Callable[..., str], *args) -> Callable[..., str]:
# Body
Upvotes: 139
Reputation: 6962
Its intended use shouldn't be only for these 3rd party modules. It isn't mentioned properly in the Python documentation (or maybe I just couldn't find that) but the ellipsis ...
is actually used in CPython in at least one place.
It is used for representing infinite data structures in Python. I came upon this notation while playing around with lists.
See this question for more info.
Upvotes: 3
Reputation: 2864
You can also use the Ellipsis when specifying expected doctest output:
class MyClass(object):
"""Example of a doctest Ellipsis
>>> thing = MyClass()
>>> # Match <class '__main__.MyClass'> and <class '%(module).MyClass'>
>>> type(thing) # doctest:+ELLIPSIS
<class '....MyClass'>
"""
pass
Upvotes: 49
Reputation: 101156
You can use Ellipsis yourself, in custom slicing situations like numpy has done, but it has no usage in any builtin class.
I don't know if it was added specifically for use in numpy, but I certainly haven't seen it used elsewhere.
See also: How do you use the ellipsis slicing syntax in Python?
Upvotes: 14