In this hands-on tutorial we will reinvent the wheel and implement the module itertools
in plain Python.
We'll do that because that will teach us a lot about Python:
- how generators and lazy iteration work;
- the difference between iterables and iterators;
- what the built-ins
iter
andnext
do; - how Python handles iteration and
for
loops under the hood; - what the iterator protocol is;
- how the dunder methods
__iter__
and__next__
play a part in all this; and - we'll also learn about the functions inside
itertools
, a module with plenty of useful tools for your day-to-day iterations.
And most importantly, we'll do it because it's genuinely fun!