Urban Ishimwe’s Software Dev Journey
Perhaps in another life, Urban Ishimwe might have been some famous Social Media Influencer helping b...
Before we get into the topic, let’s get some definitions right first:
To better illustrate, consider a list:
And so on, till we have gotten all items from the list after which the StopIteration exception is raised. By the way, we can also call iter_obj.__next__()
In order to build your own iterators, you have to implement a class with the __iter__ and __next__ methods. At times this can be very cumbersome, and that is where generators come in. Generators allow us to easily create iterators. All implementations mentioned above are not required as they are automatically handled. It’s fairly simple to create a generator in python: as easy as defining a normal function with the yield statement rather than the return statement. If you’re wondering what’s the difference between these two terms; return terminates a function while yield pauses the function while saving its state and will continue from where it left off on successive calls.
As an example, we can define a simple function that is a generator for Fibonacci numbers. (Some people might argue that the sequence starts from 0 but let’s focus on the python here :
Simple, isn’t it? On to coroutines now. Please note that in this article I talk about simple coroutines rather than native coroutines.
We’ve seen that generators allow us to pull data and pause execution from a function context. Coroutines allow us to push data. In this case, the yield statement basically means “Wait until you get some input data”. We also use the yield statement a bit different as we shall see in a bit.
We’ll go through a very simple example which provides a good basis: Imagine we have a list of names and want to find out if some names we have in mind (we don’t know how many since they’ll be input at runtime) are in this list.
We can now do:
However, a coroutine can’t start receiving data right away and we’ll get an error when we try to do so. We first need to prime it. This can be done easily by doing:
Now, we can start sending some data to our coroutine, and this is done by making use of the send method.
An important thing to note is if our generator or coroutine does not have a breakpoint, we can manually stop it by doing:
That’s it for now, I hope this was helpful in beginning to understand generators and coroutines. You can read more from these resources:
READ: How to build your own version of React from scratch (Part 1)
Perhaps in another life, Urban Ishimwe might have been some famous Social Media Influencer helping b...
The app developed here will help a user to view their phone number and account details (Account numb...
I have worked remotely as a Software engineer with several companies for over four years now. This a...
We’re excited to announce that we will be hosting the CodeNaija 2019 hackathon event in partnershi...
Before jumping into the gist of this article it is important to understand what it hopes to achieve ...
We started Andela five years ago to solve a simple but pervasive global challenge: Brilliance is eve...
Before we get into the topic, let’s get some definitions right first:
To better illustrate, consider a list:
And so on, till we have gotten all items from the list after which the StopIteration exception is raised. By the way, we can also call iter_obj.__next__()
In order to build your own iterators, you have to implement a class with the __iter__ and __next__ methods. At times this can be very cumbersome, and that is where generators come in. Generators allow us to easily create iterators. All implementations mentioned above are not required as they are automatically handled. It’s fairly simple to create a generator in python: as easy as defining a normal function with the yield statement rather than the return statement. If you’re wondering what’s the difference between these two terms; return terminates a function while yield pauses the function while saving its state and will continue from where it left off on successive calls.
As an example, we can define a simple function that is a generator for Fibonacci numbers. (Some people might argue that the sequence starts from 0 but let’s focus on the python here :
Simple, isn’t it? On to coroutines now. Please note that in this article I talk about simple coroutines rather than native coroutines.
We’ve seen that generators allow us to pull data and pause execution from a function context. Coroutines allow us to push data. In this case, the yield statement basically means “Wait until you get some input data”. We also use the yield statement a bit different as we shall see in a bit.
We’ll go through a very simple example which provides a good basis: Imagine we have a list of names and want to find out if some names we have in mind (we don’t know how many since they’ll be input at runtime) are in this list.
We can now do:
However, a coroutine can’t start receiving data right away and we’ll get an error when we try to do so. We first need to prime it. This can be done easily by doing:
Now, we can start sending some data to our coroutine, and this is done by making use of the send method.
An important thing to note is if our generator or coroutine does not have a breakpoint, we can manually stop it by doing:
That’s it for now, I hope this was helpful in beginning to understand generators and coroutines. You can read more from these resources:
READ: How to build your own version of React from scratch (Part 1)
Tap into a global talent pool and hire the “right” developers in days, not months.
Accelerate your career by working with high-performing engineering teams around the world.
BECOME A DEVELOPERWe take great pride in matching our developers with the best partners. Tell us about your team below!