Using Yield in your python functions

A way to create generators or to use it for infinite numbers.

A useful trick to use when creating functions or just when you don’t want to create a computationaly heavy item you can use the yield object to produce a generator that can return an object on demand.

A perfect example to illustrate how yield can work is by using the Sieve of Eratosthenes example. How the sieve works is by taking a number and removing all the numbers ahead of it that are divisible by said number. We keep going until we have an infinite amount of prime numbers.

This can be very computational intensive but if we want to compute the entire list it would be impossible! By taking away the infinite sequences of numbers and the computational power that it would require, yield can bring a solution to that.

Let’s start with a simple example. We will create a function, and in that function we will tell it to yield the inputted number. Let’s take a look!

Now, we can see that if we place an input, it returns an object. This is because yield is not a regular function. It’s a generator object and, just like any object, needs to be assigned to a variable. Once we do that, we can call our output by using the next() function. Let’s take a look.

Nice! We inputted 2 and when we used next() it returned us our input. But what if we call this again? let’s see what happens.

We got an error. This is because, how yield works, is by stopping the computation that the function performs and returning item by item whenever the next() function is called. Since we just coded to yield n nothing else is being generated, therefore yield completed its task.

Let’s fix this by actually creating a generator. We accomplish this by telling it to yield from. How this works is we tell the function to call back the function and hold the next computation until it’s called. It sounds complicated but let’s look at an example.

Look at that! we have a generator. How this works is, we first provide n (the original number) and we yield this number. Yield holds to that number until it’s called again. When we call that number again, yield proceeds to use the yield from function and generates us the next number and cease computation. This stores the number generated. Since we’re calling back the function again in the yield from part and it will keep providing us numbers as long as we keep calling next().

Let’s now compute the sieve! We create the function for the sieve. Then, the first number is stored it into a variable. After, we yield that number. Once that number is stored we want to be able to remove the multiples of that number from our sieve, to do so, we need to yield from it and use the sieve to remove all the multiples of it. We will use comprehension to do so. Let’s look at an example.

Look at that! we were able to create The Sieve of Eratosthenes in a very simple line of code! Feel free to check it out and try it out on your own! I’ll type out the code down below as well!

#Creating a number generator that will yield a consecutive number.
def nats(n):
yield n
yield from nats(n+1)
#Creating the sieve by taking the number generator and joining it with the yield of the sieve.
def sieve(s):
n = next(s)
yield n
yield from sieve (i for i in s if i%n != 0)
#Instantiating the sieve and providing the seed.
nat = sieve(nats(2))
#Calling each number of the infinite list of natural numbers.
next(nat)

A Data Scientist in the making!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store