Resource mentioned in this lesson:
Introducing Async Iteration
00:00 So now that you are familiar with async programming, you’re moving on to the introduction to asynchronous iterators and iterables. Now, to start with though, forget asynchronous programming for a second and just focus on the concepts of iterators and iterables.
00:18 Now, what is an iterable? An iterable you can think of as a container of data that can be iterated over, such as a list, for example. And what is an iterator?
00:30 Well, an iterator you can think of as the object that controls the iteration process over your iterable. Okay, so what does that mean in terms of implementation?
00:39
Well, in terms of implementation, an iterable implements the __iter__() special method. An iterator then implements the __iter__() and the __next__() special methods.
00:52 Now, if you need a little bit more help with this, I include a link to a Real Python tutorial that explains iteration in a bit more detail.
01:01 Now, if you bring back the idea of async programming, you then get the async iterators and iterables. So an async iterable holds data to iterate over asynchronously, and async iterators then control the iteration process over such asynchronous iterable.
01:19
And in terms of implementation, the async iterable implements the __aiter__() special method and an async iterator implements the __aiter__() and the __anext__() special methods.
01:33
Okay, now to bring this concept a bit more to life, I’d like you to think back of a list, a list which you know is an iterable. So when I invite you to open up your REPL and apply dir() to the list object, and when you hit Enter, you’ll see a whole array of methods and special methods, one of which is the __iter__() special method.
01:56
So the list object is an iterable because it implements the __iter__() special method. That is what that means.
02:04
Now, if you look again, can you find the __aiter__() special method? Well, I can’t see it. So a list is not an async iterable. It’s an iterable, but not an async iterable.
02:16
You are going to have to build your own. So in the next lesson, you’ll cover the implementation of the __aiter__() and __anext__() special methods.
Become a Member to join the conversation.
