Python: Generator and yield - wild-blooming/jot-down GitHub Wiki

function:work done by the fun and stored local var is lost when control is returned to the caller.

generator: return next val instead of all the val at once. yield: pass a val to who called next(),save state of generator. return an intermediate result to caller,maintaining local state,resuming where it left off.

producer/consumer function

when generator is called: no code in body is executed; a generator-iterator obj is returned; each time next() invoked,code in body is executed until 'yield' or 'return' or until the end of the body.

yield not allowed in try/finally,no guarantee generator will be resumed.