Python: Function to flatten generator containing another generator

Viewed 3408

I would like to know how to write python function which can flatten generator which yields another generators or iteables (which can also yield another generators/iterables ... possibly infinitely).

Here is example:

gen(gen(1,2,3), gen(4,5,6), [7,8,9], [gen(10,11,12), gen(13,14,15)])

note: gen - means generator object, content between parentheses after gen is data that will generator gen yield.

The expected result after "flattening": gen(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)

It is necessary for the flatten function to return generator too! (because otherwise, the preceeding usage of generators would be meaningless).

Just to note, I am using python 3.

Thanks!

2 Answers
Related