Python产量

本文深入探讨Python的yield关键字,它用于逐个返回函数值,尤其适用于处理大量数据时节省内存。通过示例展示了如何使用yield创建生成器,并解释了其工作原理和内存优势。
摘要由CSDN通过智能技术生成

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关键字。

翻译自: https://www.journaldev.com/15809/python-yield

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值