Generators, Iterables and Iterators

Iterables and iterators

The for loop calls the __iter__() method on the iterable which returns an iterator object.

The iterator object contains implementations of __iter__ method which returns self. And __next__ method which does the looping logic such as so:

index+= 1

return data_of_iterable[index]

The for loop takes the iterator object, and calls __next__. The for loop takes the value returned from the __next__ method and sets that value as the for loop variable (ie. item | for item in items)

The for loop then runs the code block within the for loop.

Afterwards, the for loop then calls the __next__ method again and again until the stop condition is reached.

Generators

Action of Generators in a for loop

A generator is similar to the iterator object in that it has implementations of __iter__ and __next__ method. The __iter__ method returns self.

The for loop calls the __iter__ method on the generator, returning self (the generator).

The for loop calls the __next__ method, which does the looping logic such as so:

current += 1

return current

Afterwards, the for loop then calls the __next__ method again and again until the stop condition is reached.

Creating generators

It is possible to create a generator object manually by implementing a class with the __iter__ and __next__ method.

For example:

However, a faster way to create a generator object is to simply use the ‘yield’ keyword in a function. The following demonstrates the use of ‘yield’ keyword and the generator object it creates under the hood.

Crossover between iterables and generators

Realising that the iterator object is rather similar to a generator object, we can have a custom implementation of the __iter__ method which returns a generator object instead of a iterator object and the iterable would still work the same.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store