Vzz Talks
Vzz Talks

Let's talk about advanced things in simple ...

May 2021
« Jan    


Handy Python Features – Part 03




Last time we had a look at how decorators work in Python. You can find the previous discussion from the following links.

Handy Python Features — Part 01

Handy Python Features — Part 02

It is time for us to explore another handy feature in Python. We will discuss what are the Generators and how they work.

Generator feature has been originally proposed to the language with the Python Enhancement Proposal — 255 (PEP 255). The functionality is a bit similar to Python Iterators which has been proposed viaPEP 234.

We will first understand how Iterators work.


Suppose a salesman having a refrigerator filled with Ice creams and some kids waiting for them. The salesman is taking care of the distribution task based on the demand. Here, the salesman is acting like an Iterator who is responsible for taking out Ice creams from the container, the refrigerator.

With this knowledge, we will define the Iterator as a caretaker of producing values from its container on demand.

Now the salesman finds it boring and fixes a machine to the refrigerator which can issue an Ice cream on demand acting as the Iteratorinstead of him. Since the refrigerator is modified and having a new capability of producing an issuer or the Iteratorit can be called as an Iterablehereafter.

Functionality of Iterators

In Python, an Iterable should return an Iterator via the method__iter__(self). Similarlyan Iterator should return the next value via the method __next__(self). An Iterator should also be an Iterableby the definition. Most of the cases, Iterators return self via the method__iter__(self) in order to achieve this.

Following example shows the above story implemented in Python.

The method iter(refrigerator) returns the Iterator, the object issuer_machine and the method next(issuer_machine) returns a stored Ice cream. Those methods calls the methods __iter__(self) and __next__(self) respectively. Following is the resulting Ice cream distribution.

Iterators: issuing Ice creams

Notice the error StopIteration() israised once the store is empty.

Iterators in use

Iterators can be used in looped and comprehensions as well. We will discuss more on comprehensions in a coming article though the following example shows both for the completeness.

The output is as follows.

Iterators: output for both loop and comprehension

Suppose storing Ice creams is costly. It requires more space. We can save this space by making one when requested by a kid. Salesman now decides to replace whole the refrigerator and the issuer machine with an ice-cream-making machine.

Iterator modification

We can achieve this by using an Iterator as in the following example.

The object ice_cream_making_machine is making Ice creams until it finds the raw material empty. The results are as follows.

Iterators: making Ice creams on demand

Now we are ready to explore on Generators since we have the basic idea of how Iterators work.


Generators are doing the same task as we saw in the above example. The difference is simply the Generators can be implemented using just a function. These functions do not use return statements. There is a special keyword called yield used instead.

Generator implementation

We will convert the above example to a Generator as follows.

The Generator make_ice_creams(raw_material) returns a new Ice cream when one is requested. When the raw materials are empty, an error StopIteration() is raised as in Iterators.

Generators in use

As discussed in the Iterators section, Generators can also be used in loops and comprehensions. The following example shows both usages.

The output is as follows.

Generators: output for both loop and comprehension

There is no difference in the output as we saw in the above Iterator discussion.

Generator expressions

There is another way of writing Generators without even using a function with the keyword yield. That is Generator expressions which are similar to comprehensions.

We will explore comprehensions more in a coming article. But for a start, we will make a list of five Ice cream objects with list comprehensions as follows.

We can use this Ice cream list to serve the kids when they request them one by one. Again the space problem comes into the picture. Can we convert this list to a generator?

Yes, as we did in the list comprehensions, a single statement would do the job. The only change we have to do is using parenthesis instead of square brackets.

Further reading …

Generators can increase performance. I would recommend you to read more about this. We will discuss performance in the coming articles.


Technical Team Leader @CodeGen

Comments 0
There are currently no comments.