In this article we will discuss how to make your custom class Iterable and also create Iterator class for it.

Why should we make a Custom class Iterable ?

Custom classes created by us are by default not Iterable. If we want to iterate over the objects of our custom class then we need to make them Iterable and also create Iterator class for them.
Let’s under stand by example,

Suppose we have a class Team, that basically contains lists of Junior & senior team members i.e.

Now let’s create an object of this class and add some junior and senior team members in it i.e.

Till now this class is not Iterable, therefore if we call iter() function on the object of this class i.e.

or try to iterate over this class’s object using for loop i.e.

Then it will throw following error i.e.

So, to iterate over the elements of class Team using its object we need to make class Team Iterable.

How to make your Custom Class Iterable | The Iterator Protocol

To make your class Iterable we need to override __iter__() function inside our class i.e.

This function should return the object of Iterator class associated with this Iterable class.

So, our Iterable team class will be like this,

It overrides the __iter__() function. Which returns the object of Iterator class i.e. TeamIterator in our case.

If we call the iter() function on the object of class Team, then it in turn calls the __iter__() function on team object. Which returns the object of Iterator class TeamIterator i.e.

Output:

Now let’s see how to create an Iterator class that can Iterate over the contents of this Iterable class Team.

How to create an Iterator class

To create an Iterator class we need to override __next__() function inside our class i.e.

__next__() function should be implemented in such a way that every time we call the function it should return the next element of the associated Iterable class. If there are no more elements then it should raise StopIteration.

Also, Iterator class should be associated with Iterable class object in such a way so that it can access the data members of Iterable class.
Usually inside __iter__() function while creating the object of Iterator class, the Iterable class passes the reference of its current object in Iterator’s constructor. Using which Iterator class object can access the Iterable class’s data members.

Let’s create TeamIterator class for the Iterable Team class i.e.

It accepts a Team class object in its constructor and inside __next__() function returns the next element from Team class object’s data members i.e. _juniorMembers & _seniorMembers in a sequence.

Now you can iterate over the contents of Team class using Iterators i.e.

Output:

How did it worked ?

iter() function calls the __iter__() on team objects, which returns the object of TeamIterator. Now on calling next() function on TeamIterator object, it internally calls the __next__() function of the TeamIterator object, which returns the next member details every time. It uses the _index variable to keep the track of already iterated elements. So, every time it is called, it returns the next element and in the end raises StopIteration.

Now as our Team class is Iterable, so we can also iterate over the contents of Team class using for loop too i.e.

Output:

Complete example is as follows:

Output:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,