Skip to content

Commit 85352fc

Browse files
committed
Add example: Ellipsis
Resolves satwikkansal#96
1 parent a33f715 commit 85352fc

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,66 @@ Try block executed successfully...
22032203
- `else` clause after try block is also called "completion clause" as reaching the `else` clause in a `try` statement means that the try block actually completed successfully.
22042204
22052205
---
2206+
### ▶ Ellipsis ^
2207+
2208+
```py
2209+
def some_func():
2210+
Ellipsis
2211+
```
2212+
2213+
**Output**
2214+
```py
2215+
>>> some_func()
2216+
# No output, No Error
2217+
2218+
>>> SomeRandomString
2219+
Traceback (most recent call last):
2220+
File "<stdin>", line 1, in <module>
2221+
NameError: name 'SomeRandomString' is not defined
2222+
2223+
>>> Ellipsis
2224+
Ellipsis
2225+
```
2226+
2227+
#### 💡 Explanation
2228+
- In Python, `Ellipsis` is a globally available builtin object which is equivalent to `...`.
2229+
```py
2230+
>>> ...
2231+
Ellipsis
2232+
```
2233+
- Eliipsis can be used for several purposes,
2234+
+ As a placeholder for code that hasn't been written yet (just like `pass` statement)
2235+
+ In slicing syntax to represent the full slices in remaining direction
2236+
```py
2237+
>>> import numpy as np
2238+
>>> three_dimensional_array = np.arange(8).reshape(2, 2, 2)
2239+
array([
2240+
[
2241+
[0, 1],
2242+
[2, 3]
2243+
],
2244+
2245+
[
2246+
[4, 5],
2247+
[6, 7]
2248+
]
2249+
])
2250+
```
2251+
So our `three_dimensional_array` is an array of array of arrays. Let's say we want to print the second element (index `1`) of all the innermost arrays, we can use Ellipsis to bypass all the preceding dimensions
2252+
```py
2253+
>>> three_dimensional_array[:,:,1]
2254+
array([[1, 3],
2255+
[5, 7]])
2256+
>>> three_dimensional_array[..., 1] # using Ellipsis.
2257+
array([[1, 3],
2258+
[5, 7]])
2259+
```
2260+
Note: this will work for any number of dimensions. You can even select slice in first and last dimension and ignore the middle ones this way (`n_dimensional_array[firs_dim_slice, ..., last_dim_slice]`)
2261+
+ In [type hinting](https://docs.python.org/3/library/typing.html) to indicate only a part of the type (like `(Callable[..., int]` or `Tuple[str, ...]`))
2262+
+ You may also use Ellipsis as a default function argument (in the cases when you want to differentiate between the "no argument passed" and "None value passed" scenarios).
2263+
2264+
2265+
---
22062266
22072267
### ▶ Inpinity *
22082268

0 commit comments

Comments
 (0)