functions and functional programming inside the generator functionclose(is signaled by generatorexit exception occurring on the yield statement you can optionally catch this exception to perform cleanup actions def countdown( )print("counting down from %dntrywhile yield except generatorexitprint("only made it to %dnalthough it is possible to catch generatorexitit is illegal for generator function to handle the exception and produce another output value using yield moreoverif program is currently iterating on generatoryou should not call close(asynchronously on that generator from separate thread of execution or from signal handler coroutines and yield expressions inside functionthe yield statement can also be used as an expression that appears on the right side of an assignment operator for exampledef receiver()print("ready to receive"while truen (yieldprint("got %sna function that uses yield in this manner is known as coroutineand it executes in response to values being sent to it its behavior is also very similar to generator for exampler receiver( next(advance to first yield ( _next_ (in python ready to receive send( got send( got send("hello"got hello in this examplethe initial call to next(is necessary so that the coroutine executes statements leading to the first yield expression at this pointthe coroutine suspendswaiting for value to be sent to it using the send(method of the associated generator object the value passed to send(is returned by the (yieldexpression in the coroutine upon receiving valuea coroutine executes statements until the next yield statement is encountered the requirement of first calling next(on coroutine is easily overlooked and common source of errors thereforeit is recommended that coroutines be wrapped with decorator that automatically takes care of this step lib fl ff