In this intermediate python tutorial series, this time we will be learning about Python Generator, syntax, and how python generators are used to create an iterator object.
To understand Python Generators you should have basic knowledge of python, its syntax, and also the behavior of functions in python.
Generators in Python
A Generator in Python is a sequence creation object i.e iterator. The Generated Iterator can contain a huge sequence.
Even though the generated sequence is pretty huge, it can be successfully iterated.
This is possible because the generator does not create and store the sequences in the memory at once, but it creates the sequence on the fly. This also saves memory usage of the program.
Creation of an iterator in python is a pretty heavy task, we have to create a class with __iter__() and __next__() methods, and also have to handle errors.
These steps are automatically performed by the Python Generator and return an iterator.
That’s why python generators have become so popular.
Generator Creation in Python
In this example, we are going to make a ranger generator function. This will be the copy of the range() function in python.
range() the function also returns a generator object and then it can be iterated.
r = ranger(0, 10) print(type(r))
The Below code defines a ranger function. It takes three default parameters (start, end, and step). Till the start is less than the end value, the value is yielded and the stat value is increased by the step value.
As in normal functions we use the return keyword to return the value from the function. In a generator function, the return keyword is replaced by the yield keyword.
def ranger(start=0, end=0, step=1): while start < end: yield start start += step
Let’s see what is the type of ranger.
The ranger is a type as you would have known earlier. Now let’s see what the ranger function returns.
As you have guessed, the ranger returned object is a generator.
The generator in python returns an iterable, let’s iterate it.
for i in ranger(0, 10): print(i)
0 1 2 3 4 5 6 7 8 9
A thing to notice is that the Python Generator can only be iterated once. If you try to iterate the generator again, it will not work.
This is because the python generator generates the iterable sequence on the fly and once iterated can be restored.
r = ranger(0, 10) for i in r: print(i) print("Printing again") for t in r: print(t)
0 1 2 3 4 5 6 7 8 9 Printing again
The next loop doesn’t print any value as the python generator can only be iterated once.
Generator Next Function
The Next function is used to get the next yielded value from the python generator.
In the above example, we used the for loop to iterate over the generator object i.e iterator, the Stop Iteration Exception is handled by the for loop automatically. But when we iterate manually using the __next__() function. We have to keep the exception in mind.
r = ranger(0, 4) print(r.__next__()) print(r.__next__()) print(r.__next__()) print(r.__next__())
0 1 2 3
If we get over the end value an exception will be thrown by the generator object i.e StopIteration Exception.
r = ranger(0, 4) print(r.__next__()) print(r.__next__()) print(r.__next__()) print(r.__next__()) print(r.__next__())
0 1 2 3 Traceback (most recent call last): File ".\sr.py", line 13, in print(r.__next__()) StopIteration
Hope you like it!