Skip to content

Commit c5665da

Browse files
committed
Nitpicks in the new example
1 parent 0fa3067 commit c5665da

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

README.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,57 +1888,60 @@ tuple()
18881888
18891889
1\.
18901890
```py
1891-
def func(k):
1892-
if k == 3:
1893-
return ["A string..."]
1891+
def some_func(x):
1892+
if x == 3:
1893+
return ["wtf"]
18941894
else:
1895-
yield from range(k)
1895+
yield from range(x)
18961896
```
18971897
18981898
**Output:**
18991899
```py
1900-
>>> list(func(3)) # expected: ["A string..."]
1900+
>>> list(some_func(3))
19011901
[]
19021902
```
19031903
1904-
The same behavior is true if we rewrite `yield from` as a for loop.
1904+
Where did the `"wtf"` go? Is it due to some special effect of `yield from`? Let's validate that,
19051905
19061906
2\.
19071907
```py
1908-
def func(k):
1909-
if k == 3:
1910-
return ["A string..."]
1908+
def some_func(x):
1909+
if x == 3:
1910+
return ["wtf"]
19111911
else:
1912-
for j in range(k):
1913-
yield j
1912+
for i in range(x):
1913+
yield i
19141914
```
19151915
19161916
**Output:**
19171917
```py
1918-
>>> list(func(3)) # expected: ["A string..."]
1918+
>>> list(some_func(3))
19191919
[]
19201920
```
19211921
1922+
Same result, that didn't work either.
1923+
19221924
#### 💡 Explanation:
19231925
1924-
Starting from Python 3.3+ it became possible to use return statement
1925-
with values inside generators ([PEP380](https://www.python.org/dev/peps/pep-0380/)). The [official doc](https://www.python.org/dev/peps/pep-0380/#enhancements-to-stopiteration) says that "... `return expr` in a generator causes `StopIteration(expr)` to be raised upon exit from the generator."
1926+
+ From Python 3.3 onwards, it became possible to use `return` statement with values inside generators (See [PEP380](https://www.python.org/dev/peps/pep-0380/)). The [official docs](https://www.python.org/dev/peps/pep-0380/#enhancements-to-stopiteration) say that,
19261927
1927-
So, to get `["A string..."]` from the generator `func` we need to catch `StopIteration`, e.g.:
1928+
> "... `return expr` in a generator causes `StopIteration(expr)` to be raised upon exit from the generator."
19281929
1929-
```py
1930-
try:
1931-
next(func(3))
1932-
except StopIteration as e:
1933-
string = e.value
1934-
```
1930+
+ In case of `some_func(3)`, `StopIteration` is raised at the beginning because of `return` statement. The `StopIteration` exception is automatically catched inside the `list(...)` wrapper and the `for` loop. Therefore, the above two snippets result in an empty list.
19351931
1936-
```py
1937-
>>> string
1938-
['A string...']
1939-
```
1932+
+ To get `["wtf"]` from the generator `some_func` we need to catch the `StopIteration` exception,
1933+
```py
1934+
try:
1935+
next(some_func(3))
1936+
except StopIteration as e:
1937+
some_string = e.value
1938+
```
1939+
1940+
```py
1941+
>>> some_string
1942+
["wtf"]
1943+
```
19401944
1941-
Note that `list(...)` automatically catches `StopIteration`. In case of `func(3)` `StopIteration` raises at the beginning because of `return` statement. Therefore, `list(func(3))` results in an empty list.
19421945
19431946
---
19441947

0 commit comments

Comments
 (0)