Lesson 18. Generators
To understand Generator, you should be familiar with Iterator first, because Python Generator is an elegant approach to create an iterator. An Iterator in a nutshell is pointer that points to first element of a collection like list, tuple, or dictionary. Iterator is a class with iter() and next() methods. When we pass iterator object to python's next() function, it points to next element in that collection. When end of iterator is reached an StopIteration is raised, letting you know that are are no further elements. You already see the pain in implementation. This is where the saviour Generator comes into play.
So, a Generator is a function that that uses yield keyword to create an object that could be iterated using next() function. Generator is just like other python functions, but with yield statment instead of return. The difference here is that when return statement is executed, the function stops execution right away which is not the case with yield statement. It keeps generating values and resumes on successive calls. The Generator remembers all local variables when called by subsequent call.
Now, you must be wondering how generators are different from comprehension (Lesson no 19). Well, comprehensions produce the entire list, but generators only give you one element at a time, this is known as lazy operation. Isn't it memory ifficient?
There are two different ways you can create generators in python.
Generators can be created like normal functions with only modification of using yield statment instead of return statement. This is the common way of creating generators.
Generators can be created using comprehension like method using parenthesis instead of square brackets.
Generators Syntax
Examples
Example #1: Function Method
Let's create an iterator using Generator by reversing Some company's revenue in million dollars for one week.
Example #2: Expression Method
Using the expression method.
Now you try it!
Don't copy and past. Type the code yourself!
Last updated