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
iterandnextdo; - how Python handles iteration and
forloops 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!