August 2018
Intermediate to advanced
332 pages
9h 12m
English
In its most basic form, the new yield from syntax can be used to chain generators from nested for loops into a single one, which will end up with a single string of all the values in a continuous stream.
The canonical example is about creating a function similar to itertools.chain() from the standard library. This is a very nice function because it allows you to pass any number of iterables, and will return them all together in one stream.
The naive implementation might look like this:
def chain(*iterables): for it in iterables: for value in it: yield value
It receives a variable number of iterables, traverses through all of them, and since each value is iterable, it supports a for... in.. construction, so ...