![age progression generator age progression generator](https://d3i71xaburhd42.cloudfront.net/2a38330df8c2bd737e723d78d52baa3b296ecb5f/4-Figure2-1.png)
Simple generators can be easily created on the fly using generator expressions. Note: This generator function not only works with strings, but also with other kinds of iterables like list, tuple, etc. In this example, we have used the range() function to get the index in reverse order using the for loop. Let's take an example of a generator that reverses a string. Normally, generator functions are implemented with a loop having a suitable terminating condition. The above example is of less use and we studied it just to get an idea of what was happening in the background. When you run the program, the output will be: This is printed first Check here to know how a for loop is actually implemented in Python. It automatically ends when StopIteration is raised. This is because a for loop takes an iterator and iterates over it using next() function.
![age progression generator age progression generator](https://i0.wp.com/www.venostech.com/wp-content/uploads/2021/05/AgingBooth.jpg)
One final thing to note is that we can use generators with for loops directly. To restart the process we need to create another generator object using something like a = my_gen(). Furthermore, the generator object can be iterated only once. Unlike normal functions, the local variables are not destroyed when the function yields. One interesting thing to note in the above example is that the value of variable n is remembered between each call. > # Finally, when the function terminates, StopIteration is raised automatically on further calls. > # Local variables and theirs states are remembered between successive calls. > # Once the function yields, the function is paused and the control is transferred to the caller. > # We can iterate through the items using next(). > # It returns an object but does not start execution immediately. Run these in the Python shell to see the output. # Generator function contains yield statementsĪn interactive run in the interpreter is given below. We have a generator function named my_gen() with several yield statements. Here is an example to illustrate all of the points stated above. Finally, when the function terminates, StopIteration is raised automatically on further calls.Local variables and their states are remembered between successive calls.Once the function yields, the function is paused and the control is transferred to the caller.So we can iterate through the items using next(). Methods like _iter_() and _next_() are implemented automatically.When called, it returns an object (iterator) but does not start execution immediately.Generator function contains one or more yield statements.Here is how a generator function differs from a normal function. The difference is that while a return statement terminates a function entirely, yield statement pauses the function saving all its states and later continues from there on successive calls.ĭifferences between Generator function and Normal function Both yield and return will return some value from a function. If a function contains at least one yield statement (it may contain other yield or return statements), it becomes a generator function. It is as easy as defining a normal function, but with a yield statement instead of a return statement. It is fairly simple to create a generator in Python. Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time). All the work we mentioned above are automatically handled by generators in Python. Python generators are a simple way of creating iterators. Generator comes to the rescue in such situations. This is both lengthy and counterintuitive. We have to implement a class with _iter_() and _next_() method, keep track of internal states, and raise StopIteration when there are no values to be returned. There is a lot of work in building an iterator in Python.