Generators
Last updated
Last updated
Object that enables a programmer to traverse a container, particularly lists. However, an iterator performs traversal and gives access to data elements in a container, but does not perform iteration. You might be confused so lets take it a bit slow. There are three parts namely:
Iterable
Iterator
Iteration
All of these parts are linked to each other.
Any object in Python which has an __iter__
or a __getitem__
method defined which returns an iterator or can take indexes (You can read more about them ).
Any object which can provide us with an iterator.
Any object in Python which has a next
(Python2) or __next__
method defined.
The process of taking an item from something e.g a list.
Generators are iterators, but you can only iterate over them once
They do not store all the values in memory, they generate the values on the fly.
You use them by iterating over them, either with a ‘for’ loop or by passing them to any function or construct that iterates.
Most of the time generators
are implemented as functions. However, they do not return
a value, they yield
it.
Example of a generator
function:
Generators are best for calculating large sets of results (particularly calculations involving loops themselves) where you don’t want to allocate the memory for all results at the same time.
Example calculates fibonacci numbers:
Now we can use it like this:
This way we would not have to worry about it using a lot of resources.
We can iterate over generators
only once but we haven’t tested it.
next()
: allows us to access the next element of a sequence. So let’s test out our understanding:
After yielding all the values next()
caused a StopIteration
error (all the values have been yielded).
Do you know that a few built-in data types in Python also support iteration? Let’s check it out:
The error says that str
is not an iterator. Well it’s right! It’s an iterable but not an iterator. This means that it supports iteration but we can’t iterate over it directly. So how would we iterate over it? It’s time to learn about one more built-in function, iter
. It returns an iterator
object from an iterable. While an int
isn’t an iterable, we can use it on string!