2014-03-26 13:27:52 -04:00
|
|
|
# Case of terminating subgen using return with value
|
|
|
|
def gen():
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
return 3
|
|
|
|
|
|
|
|
def gen2():
|
|
|
|
print("here1")
|
|
|
|
print((yield from gen()))
|
|
|
|
print("here2")
|
|
|
|
|
|
|
|
g = gen2()
|
|
|
|
print(list(g))
|
|
|
|
|
|
|
|
|
2018-02-26 23:39:31 -05:00
|
|
|
# StopIteration from within a Python function, within a native iterator (map), within a yield from
|
|
|
|
def gen7(x):
|
|
|
|
if x < 3:
|
|
|
|
return x
|
|
|
|
else:
|
|
|
|
raise StopIteration(444)
|
|
|
|
|
|
|
|
def gen8():
|
|
|
|
print((yield from map(gen7, range(100))))
|
|
|
|
|
|
|
|
g = gen8()
|
|
|
|
print(list(g))
|