Exactly what it sounds like: it yield
s the values from
the other source, one at a time, until it runs out. Every time there is a request for the next
element from this generator, if there is still something in the other source, it will use that. Otherwise, the logic proceeds.
The loop
while True:
print('I am going to yield something!')
yield from normal_number_generator
is effectively equivalent to:
while True:
print('I am going to yield something!')
for i in normal_number_generator:
yield i
Because yield from
makes all of the values from normal_number_generator
get yield
ed (and thus print
ed in the main loop), but then the while True:
loop does not resume, because there are no more next
calls on that generator - so it’s “stuck” on the yield from
line. If you try next(zero_to_ten)
again after the code you showed, it will get in an infinite loop, because the while True:
loop can continue, but yield from
doesn’t have any more elements to offer, so it tries forever looking for the next actual value to yield
.
Why would it? It does its own iteration for “that iterable”, which has to finish before your while True:
loop can proceed.