Iterators, Generators and You
Submitted by Kshitij Saraogi (@kshitij10496) on Thursday, 31 August 2017
Iterators in Python aren’t a matter of type but of the protocol i.e. any class that implements this protocol can be iterated over. This talk will present an overview of the all-encompassing iteration features in Python and how the Iterator pattern is built into the Python language so you never need to implement it by hand.
Iteration is fundamental to data processing and when scanning datasets that don’t fit in memory (say of size 1TB), we need a way to fetch the items lazily, that is, one at a time and on demand. This saves memory and may avoid useless processing as well. And this exactly is what the Iterator pattern about.
Python Generators are often considered a somewhat advanced topic, but they are actually very easy to understand once you start using them on a regular basis. This talk will also introduce you to a pragmatic use-case of generators in Python codebase.
The presentation will lay the foundations of building concepts related to iteration and proceed to empower every developer to leverage their power. Here is the walkthrough all the topics I intend to cover:
Basic primer of different ways one can iterate over the built-in type objects
What are the differences between the terms iterable, iterator and generator?
Examples of iterable, iterator, generator function and tuple comprehensions
How can one make user-defined objects iterable?
What are the Iterable and Iterator interfaces?
How to implement the Iterator protocol?
How can one leverage the power of a generator to implement the Iterator protocol?
Additional benefits of making our objects iterable.
built-in functions which consume iterables
Few itertools examples
Any person who meets all the following criteria should be able to follow this talk with ease:
You have written at least a single Python for loop.
You know how to write a class in Python
You have heard about generators and would like to know where you can potentially use them
Interested in writing cleaner and Pythonic code
My name is Kshitij Saraogi. I am a Fourth-year undergraduate at Indian Institute of Technology Kharagpur, India pursuing Mathematics and Computing. I have been programming in Python more than 2 years now and have been contributing to the Open Source community for the past couple of years. I was a Google Summer of Code Student in 2017 under SymPy where I worked on equation solving modules.