I haven't been able to find any examples of return values from the yield from
expression. I have tried this simple code, without success:
def return4():
return 4
def yield_from():
res = yield from range(4)
res = yield from return4()
def test_yield_from():
for x in yield_from():
print(x)
test_yield_from()
Which produces:
» python test.py
0
1
2
3
Traceback (most recent call last):
File "test.py", line 52, in <module>
test_yield_from()
File "test.py", line 48, in test_yield_from
for x in yield_from():
File "test.py", line 44, in yield_from
res = yield from return4()
TypeError: 'int' object is not iterable
But I was expecting:
» python test.py
0
1
2
3
4
Because, as stated in the PEP:
Furthermore, when the iterator is another generator, the subgenerator is allowed to execute a return statement with a value, and that value becomes the value of the yield from expression.
Obviously, I am not getting this explanation. How does a return
in a "subgenerator" work with regards to yield from
?