Is it possible to nest yield from expressions?
The simple form is obvious:
def try_yield1(): x = range(3) yield from x
What produces:
0 1 2
But what if I have nested generators?
def try_yield_nested(): x = [range(3) for _ in range(4)] yield from ((yield from y) for y in x)
This gives:
0 1 2 None
Why does it create None if I used yield from (even if it is nested)?
I know I can do something like:
from itertools import chain def try_yield_nested_alternative(): x = [range(3) for _ in range(4)] yield from chain.from_iterable(x)
Which gives the same conclusion as None (which I expect). I can also write a simple loop:
for x in [range(3) for _ in range(3)]: yield from x
But I thought it would be more pythonic to use nested delegation (preferably even yield from x from y or yield from x for x in y , but this is not the correct syntax). Why is this not working as I expect?
source share