Skip to content
Search

Python:Ellipsis

lastmod 2021-04-18created 2021-04-181 edits

Python provides the ... operator for slicing, represented by a type called Ellipsis.

Slicing operator

Python provides a little-known slicing operator called ellipsis.

>>> class C(object):
...  def __getitem__(self, item):
...   return item
... 
>>> C()[1:2, ..., 3]
(slice(1, 2, None), Ellipsis, 3)

When you write a class that slices directly through the square bracket [ ] operation, you can also use the dot-dot-dot syntax. It is mainly used in Numpy:

import numpy as np

array = np.random.rand(2, 2, 2, 2)
print(array[..., 0])

Arbitrary-type argument

class flow:

    # (using "value: Any" to allow arbitrary types)
    def __understand__(self, name: str, value: ...) -> None: ...

Using it instead of pass

# style1
def foo():
    pass

# style2
def foo():
    ...

# both the styles are same

See also

Favorite site

author Zer0 sitemap