In the following example, we have a generator that calls another two nested generators, producing values in a sequence. Each one of these nested generators returns a value, and we will see how the top-level generator is able to effectively capture the return value since it's calling the internal generators through yield from:
def sequence(name, start, end): logger.info("%s started at %i", name, start) yield from range(start, end) logger.info("%s finished at %i", name, end) return enddef main(): step1 = yield from sequence("first", 0, 5) step2 = yield from sequence("second", step1, 10) return step1 + step2
This is a possible execution of the code in main while it's being iterated:
>>> g = main() ...