Use the first arg of StopIteration as yield from return value

This commit is contained in:
Noah
2019-10-14 21:05:39 +00:00
committed by coolreader18
parent 6f59609160
commit c31b3b2af9
3 changed files with 75 additions and 22 deletions

View File

@@ -88,3 +88,19 @@ l = list(g)
# print(l)
assert l == [99]
assert r == ['a', 66, None]
def binary(n):
if n <= 1:
return 1
l = yield from binary(n - 1)
r = yield from binary(n - 1)
return l + 1 + r
with assert_raises(StopIteration):
try:
next(binary(5))
except StopIteration as stopiter:
# TODO: StopIteration.value
assert stopiter.args[0] == 31
raise