Used as Pass Statement inside Functions
Ellipsis is used instead of pass statement inside functions. ‘pass’ replaced by ‘…’ or ‘Ellipsis’.
Example:
Python3
# style1 def foo(): pass # style2 def foo(): ... # both the styles are same |
Ellipsis can also be used as a default argument value. Especially when you want to distinguish between not passing in value and passing in None.
Example:
Python3
def foo(x = ...): return x print (foo) |
Output:
<function foo at 0x7fabdeea6e18>
Error while working with ellipsis
We should always use colons for accessing the other dimensions i.e. ‘ : ‘.
Python3
l[..., 1 ,...] |
Output:
Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: an index can only have a single ellipsis ('...')
What is Three dots(…) or Ellipsis in Python3
Ellipsis is a Python Object. It has no Methods. It is a singleton Object i.e. , provides easy access to single instances.
Various Use Cases of Ellipsis (…):
- Default Secondary Prompt in Python interpreter.
- Accessing and slicing multidimensional Arrays/NumPy indexing.
- In type hinting.
- Used as Pass Statement inside Functions.