Generators & Iterators in python
Before going ahead with the concept of Generators, it is important to get your fundamentals right about “Iterables” and ‘Iterators’. In other language, when we have to iterate over a list, we create a counter for the job. Have a look at the following example:
Another way to accomplish this, is by using range() as follows:
However the best way to do this in Python is as follows:
Iteration over string can be carried out in the following manner:
Iterations in file are also simple. Look at the following file object:
It is a text document. Now in order to read lines of this file through python we can implement the following code:
Iterables in Python are objects that can be iterated over. There are many other ways to carry out iterations.
You can use a list constructor to carry out iterations:
The following example shows how sum() function can take iterables to produce a total sum:
The next example shows how iter() function can be used for iterations:
With every next command, the next element is produced. When no more elements are left, it will display StopIteration as follows:
Coming to Generators
In the above section, you have seen various ways of carrying out iterations, however these can be further simplified by the use of Generators. A generator object is an iterator.
When a Python function is called, immediately the first line of the function is executed followed by the remaining lines of code till the point where the function ends and the control is then handed over to the caller.
We all have seen functions that return a single value at the end, but what if we have a requirement for a function that must return more than one value or a series of value? In cases where a function is expected to yield a series of values, it is important for it to save its work. Usage of ‘yield’ (as discussed in the coming examples) entails that the control is transferred temporarily and the function will reclaim it in the future.
In Python, we make use of ‘generators’ (along with ‘yield’ keyword) to achieve this ask. Generators give programmers to easily generate code which is further used to generate a series of values. With this, there is no need to have a counter to keep a track of states between calls. Simple generators are often referred to as coroutines.
The main difference between a generator and an iterable is that the latter is capable of returning only one number at a time.
Now, let’s have a look at how generators simplify the work of iteration.
When a call is made to the generator function, it first returns a generator object. By this time not even the first line of the generator has been executed. The function starts executing when the next() method is called. The function is executed till it encounters ‘yield’ and returns the value that it has received at that point. To have a better idea about how this happens, we can put some print statements in the above example:
So, generators are like a special kind of iterator that can be used to generate a series of values. The ‘yield’ in a generator function is equivalent to ‘return’ in normal function, it helps in saving the ‘state’ of the generator function. The next value of the generator can be called by using ‘next()’ function.
Enroll for Instructor Led Live Python Training