We will look into python generator today. In our previous lesson we have learnt about python iterator.
Table of Contents
- 1 Python Generator
- 2 Basic Structure of Python Generator
Python generator is one of the most useful and special python function ever. We can turn a function to behave as an iterator using python generators.
Basic Structure of Python Generator
Basically Python generator is a function. You can consider the following as the basic structure of a python generator.
#sample syntax of python generator def gereratorName(argument): #statements yield argument #statements #calling the generator variableName = gereratorName(10) print(variableName)
In the above structure you can see that everything is just like a function except one thing, that is the yield keyword. This keyword plays the vital role. Only the usage of yield, changes a normal function into a generator.
A normal function returns some value, generator yields some value. A generator automatically implements
Python generator is written like regular functions but use the yield statement whenever they want to return some data. Each time next() is called on the generator function, the generator resumes where it left off (it remembers all the data values and which statement was last executed).
Understanding Python Generator
Let’s now learn the each line of the previous code.
Line 2, is the declaration of the generator which takes an argument. This argument is optional. It depends on the programmer who will implement a generator.
Line 3, 5 mentions there may be some other statements.
Line 4 is the crucial part of the above program. It says to
yield the value of argument on the basis of some conditions that may be stated in the statements.
And line 8 is calling the generator with parameter 10 and line 9 prints the returned generator object. If you run the above program then it will output as following,
<generator object gereratorName at 0x1019d6f10>
Notice that the above output is not a value. Actually it is indicating where the object is. To get the actual value you have take help of iterator. Then implicitly next() will be called on the object to get the next value that is yielded.
If you want to print the generated values without loop then you can use next() function on it. If you add one more line in the above code like below.
Then it will output the the value 10 which was passed as argument and yielded.
Get Python Generator’s value with explicit next() call
Now take a look at the following program, where we are explicitly calling the next() function of a generator.
#fruits is a generator which generates some fruit name def fruits(): yield "Mango" yield "Jackfruit" yield "Banana" yield "Guava" #calling the generator fruit getfruits = fruits() print(next(getfruits)) print(next(getfruits)) print(next(getfruits)) print(next(getfruits))
In the above code, you have to know the exact number of values that were yielded. Otherwise you will get some error as no more value is generated from the generator function fruits().
The above code will output as following:
Mango Jackfruit Banana Guava
Get Python Generator’s value with implicit next() call
You can get the values of the generator using for loop. The following program is showing how you can print the values using for loop and generator. It will provide the same output.
#fruits is a generator which generates some fruit name def fruits(): yield "Mango" yield "Jackfruit" yield "Banana" yield "Guava" #calling the generator fruit getfruits = fruits() for a in getfruits: print(a)
Working Procedure of Python Generator
Let’s now see how the generator is actually working. Normal function terminates after the return statement but generator does not.
For the first time we call the function it returns the first value that is yielded along with the iterator. Next time when we call the generator then it resumes from where it was paused before.
All the values are not returned at a time from a generator unlike normal function. This is the speciality of a generator. It generates the values by calling the function again and again which requires less memory when we are generating a huge number of values.
Guess the output of below Python Generator Program
Let’s see another code. If you can assume the output then it’s a gain.
def timesTable(number): for i in range(1, 11): yield i * number i += 1 gettimes = timesTable(10) for a in gettimes: print(a)
The output will be:
range() is a built-in generator which generates number within the upper bound. Hope you can now write your own generator. Best of luck.
Reference: Python API Doc