There's one extra thing to mention: a function that yields doesn't actually have to terminate. I've written code like this:
def fib():
yield 1
last, cur = yield0, 1
curwhile =True: 1
last = 1
whileyield True:cur
cur, last = cur+last, cur
= cur, last yield+ cur
Then I can use it in other code like this:
for f in fib():
if some_condition: break
coolfuncs(f);
It really helps simplify some problems, and makes some things easier to work with.