Generators and comprehensions

A generator is a special kind of iterator in Python. In other words, a Python generator is a function that returns us a generator iterator by issuing the yield command, which can be iterated upon. There might be occasions in which we would want a method or function to return us a series of values, instead of just one. We might, for example, want our method to partially carry out a task, return the partial results to the caller, and then resume the work right from the place where it returned the last value. Usually, when a method terminates or returns a value, its execution begins again from the start. This is what generators try to address. A generator method returns a value and a control to the caller and then continues its execution right from where it left off. A generator method is a normal Python method with a yield statement. The following code snippet, generators.py, explains how generators can be used:

 

Note that since genMethod has a yield statement in it, it becomes a generator. Every time the yield statement is executed, the value of "a" is returned to the caller as well as the control (remember that generators return series of values). Every time the next() call is made to the generator method, it resumes its execution from where it left off previously. 

We know that every time a yield is executed, the generator method returns a generator iterator. Thus, as with any iterator, we can use a for loop to iterate over the generator method. This for loop will continue until it reaches the yield operation in the method. The same example with a for loop would look as follows:

You might be wondering why we would use generators when the same result can be achieved with lists. Generators are very memory- and space-efficient. If a lot of processing is required to generate values, it makes sense to use generators, because then we only generate values according to our requirements.

Generator expressions are one-line expressions that can produce generator objects, which can be iterated over. This means that the same optimization in terms of memory and processing can be achieved. The following code snippet shows how generator expressions can be used:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.21.5