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关键字。 Python yield的用途与return关键字的用途几乎相同,除了它逐个返回值。 当您需要返回大量值时,这是非常有用的关键字。
Python产量 (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.
python yield关键字只有一种用法,即用yield语句替换函数的return语句。
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 generator 。
Python产量示例 (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:
正如我之前所说,收益率是收益的替代。 因此yield
语句写在一个函数体中。 让我们看看如何使用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.
如果您注意的话,您将看到我们正在使用for循环查看所有值。 这意味着该函数返回可迭代的内容。 这就是为什么我们可以迭代并打印值的原因。
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.
当我们使用yield时,第一个yield值可以被迭代一次,以此类推,所有值可以被迭代一次。 一旦我们迭代了所有的yield值,就无法返回。 您可以通过使用for循环再次打印语句值来检查它。 它不会输出任何内容。 这意味着它在我们迭代时正在执行。 迭代之后,不存在任何内容。 这样可以节省存储空间。
使用yield的简单示例 (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:
我们还可以使用不同的循环编写尽可能多的yield语句。 假设我们要编写一个将提供一些值的函数。 然后该函数将每个值乘以5,然后返回所有结果。 为此,让我们看下面的示例:
# 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:
输出结果如下:
Each elements that are given as argument is now multiplied by five.
现在,将作为参数给出的每个元素乘以5。
Python产量产生器 (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:
上面示例中定义的函数称为yield生成器。 您可以通过打印命令检查如下:
# 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.
要了解有关Generator的知识,可以查看python generator教程。 当需要读取大量值时,我们使用生成器,然后使用生成器有助于节省内存。 希望本教程可以帮助您了解python yield关键字。