A generator function or generator method is one which contains a yield expression. When a generator function is called it returns an iterator. Values are extracted from the iterator one at a time by calling its_next_( ) method. At each call to_next_( ) the generator function’s yield expression’s value (None if none is specified) is returned. If the generator function finishes or executes return a Stop Iteration exception is raised.
Generators provide an elegant way to write simple and efficient code for functions that return a list of elements. Based on the yield directive, they allow you to pause a function and return an intermediate result. The function saves its execution context and can be resumed later if necessary.