Generators Homework Help
In today’s article, we are going to focus on how you can create iterators using python generators. You will learn what generators are, how they differ from the normal iterators, how to create them, and why you should use them from now onwards in your coding. However, things will not be as structured as the list suggests but upon reading this article you will get the above points which we are trying to drive home. But before we can proceed to generators, let’s start with brushing up the basic terms that will help you understand what generators are. Let’s begin with iterators.
Iteration and iterables
Iterators are one of the things that you have to grasp as they are common in all programming languages. Iterators are simply a repetition of something over and over again. In truth, if there is one place that computers have excelled and are considered to have a higher capacity is in this sector (iteration) computers can perform repetitive tasks with great accuracy that cannot be done by humans. In fact, iteration is the basis of automata which involves doing a lot of repetitive tasks. Python provides you with a way of iterating over its objects. An example is the ‘for loop’.
In most cases, iteration is done over a list but they can still be used on the data types such as strings and dictionaries. These objects that are capable of returning one of their members sequentially are known as iterables. What defines an iterable? In simple terms, we can say that there are some iteration protocols that have to be followed before an object becomes iterable. An iteration protocol is a subsidiary and related concept which this article cannot focus on. But in simple terms, we would suggest you think of it as the conditions required in a ‘for loop’ statement. One such requirement is slicing. Slicing is a programming term that implies accessing the parts of an element. An object such as lists, strings, and dictionaries can be sliced. That means we can refer to them as iterables. On the other hand objects such as integers cannot be sliced. Attempting a ‘for loop’ on integers immediately throws an error. Thus, integers are not iterable.
What are the generators in Python?
We all have to agree that the process of building an iterator is not easy. Imagine undergoing the process of implementing a class with __iter__ () and __next_ () method. Again you have to keep tabs on all the internal states and use StopIteration to raise an error in case values are not returned.
Evidently, the whole process is lengthy and counter-intuitive. This is where generators come in and simplify their work a great deal.
Python generators simplify the whole process of creating iterators.
When we discuss iterators there are two terms that are involved. These are
1. Generator-Function: this one is more like the normal function except that it needs to generate a value which it does so using the yield keyword. The normal function generates a value but uses a return keyword. Everything between a generator function is the same including the def. keyword for creating a function. The only difference is the yield keyword.
2. Generator -Object: this is a function that returns an object or simply an iterator that we can iterate over. They are used either by calling the next method on the generator object or using the generator object in a “for in” loop
How to create a generator in Python?
If you have some basic understanding of python especially in creating functions then, it’s fairly easy to create a generator in python. We can build from that and help you create generators.
In the previous section, we have noted that the difference between a normal function and a generator is the fact that the generator function uses the yield keyword. Therefore, if you create a function and have the yield keyword that is a generator function.
The two keywords always return a value when applied. But what is the difference? When a return keyword is used it terminates the function completely whereas the yield keyword pauses the function and saves what it has done which means that later it is able to continue from where it paused on successive calls.