by nstbayless · 3y ago · 0 comments
To the author: you could make the code even cleaner by moving the yield to within the action functions. Though maybe this won't work as well for parallel actions...
I suppose we could make more utilities for running coroutines "in parallel", but I haven't really felt the need. At that point we usually have to worry about exit conditions and it feels natural to just write a loop.
def first():
yield 1
yield from second()
yield 4
def second():
yield 2
yield 3
print(list(first())) # collects all the results and prints them
# Output: [1, 2, 3, 4]
But yeah, it doesn't work on a direct function call you have to know it's going to return a generator (or an iterable, like if it returns a list): def something():
yield from something_else()
def something_else():
return [1,2,3,4]