Generators are a special class of functions that allows you to easily create your own iterators. Unlike regular functions, a generator doesn’t just return a value and exit, but returns an iterator that returns the elements one at a time.
Generators are not covered in this book and are only mentioned here because they are a fairly straightforward way to create iterators. More about generators
A normal function exits if:
execution reached the
function code is ended (this works as
an exception raised
After function execution is finished the control is returned and program execution goes further. All arguments that were passed to function like local variables, all of this is lost. Only the result that returned a function remains.
A function can return a list of elements, multiple objects or different results depending on arguments, but it always returns a single result.
Generator generates values. Values are then returned on demand and after return of one value a function-generator is suspended until the next value is requested. Between requests, generator retains its state.
Python allows generators to be created in two ways:
Generator expression uses the same syntax as a list comprehensions, but returns iterator, not list (notе the parentheses instead of the square brackets):
In : genexpr = (x**2 for x in range(10000)) In : genexpr Out: <generator object <genexpr> at 0xb571ec8c> In : next(genexpr) Out: 0 In : next(genexpr) Out: 1 In : next(genexpr) Out: 4
It is useful when working with a large iterable object or infinite iterator.