Python yield

Filed Under: Python

Today we will learn about python yield keyword. Python yield has almost same purpose as return keyword except that it returns the values one by one. This is very useful keyword when you need to return a huge number of values.

Python yield

There is only one use of python yield keyword, that is to replace the return statement of a function with the yield statement.

When we do this, that function is called generator. We have already have a tutorial for generator. You can have a look at python generator.

Python yield example

As I said earlier yield is the replacement of return. So yield statement is written in the a function body. Let’s see how can we use yield:


# defining the function that will have some yield statement
def yieldStatement():
   yield 'Statement 1'
   yield 'Statement 2'
   yield 'Statement 3'

# getting the statements
statements = yieldStatement()
# for each statements that are yielded will be printed
for s in statements:
   print(s)

This will output:


Statement 1
Statement 2
Statement 3

If you notice carefully then, you will see that we are using a for loop to see all the values. That means the function return something that is iterable. That is why we can iterate it and print the values.

When we use yield, first yielded value can be iterated once, and so on all values can be iterated once. Once we iterated all the yield value then we cannot go back. You can check it by again printing the statements value using for loop. It will output nothing. That means it is executing when we are iterating it. After iterating it nothing exists. Thus it saves memory space.

Simple example of using yield

We can also write yield statements as many as we wish using different loop. Suppose we want to write a function in which we will provide some values. And the function will calculate each value times by five, then return all the resultant. To do this, Let’s have a look in the following example:


# the function that will multiply each element by 5
def multiplyByFive(*kwargs):
   for i in kwargs:
       yield i * 5

a = multiplyByFive(4, 5, 6, 8)

# showing the values
for i in a:
   print(i)

This will output as follows:

python yield

Each elements that are given as argument is now multiplied by five.

Python yield generator

The function that is defined in the above example, is called generator for the use of yield. You can check is by a print command as following:


# the function that will multiply each element by 5
def multiplyByFive(*kwargs):
   for i in kwargs:
       yield i * 5

a = multiplyByFive(4, 5, 6, 8)
print(a)

This will output:


<generator object multiplyByFive at 0x02CF7090 >

To know about generator you can have a look in the python generator tutorial. We use generator when we need to read a huge number of values, then the use generator helps to save memory. Hope this tutorial helps you in understanding python yield keyword.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages