产量 [英] Yield

查看:62
本文介绍了产量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


可能会有人解释收益率功能,请。它是如何实际工作的

你何时使用它?


感谢前进


mateusz

解决方案

Mateuszk87写道:


可能会有人解释收益率功能,请。它是如何实际工作的

你何时使用它?



它从一个函数返回一个值而没有实际终止

函数;当函数恢复时,它将继续执行

的收益率。

包含yield语句的
a函数称为生成器。 ,和$ / b $ b最常用于for-in循环,或者在期望

序列的其他上下文中。当函数

以通常方式返回时,循环自动终止:


>> def gen():



....收益率1

....收益率2

....收益率3

....


>>用于gen()中的项目:



....打印商品

....

1

2

3
< blockquote class =post_quotes>


>> sum(gen())



6


>> [str (i)for gen in gen()]



[''1'',''2'',''3'']


你也可以手动使用发电机;当你调用一个生成器

函数时,它返回一个特殊的生成器对象,然后立即暂停

。运行生成器,调用它的下一个方法:


>> g = gen()
g



< 0x00AE64E0处的生成器对象>


>> g.next()



1


> > g.next()



2


>> g.next()



3 br当生成器耗尽时,
会引发StopIterator异常:


>> g.next()



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

StopIteration


参考信息:

http://effbot.org/pyref /yield.htm


希望这会有所帮助!


< / F>


Fredrik Lundh写道:


参考信息:



请参阅:

http:// effbot.org/pyfaq/what-is-a-generator.htm


< / F>


在星期三,2006-11-15 09:13 -0800,Mateuszk87写道:


嗨。


可能有人解释收益率功能,请。它是如何实际工作的

你何时使用它?



[在

网上已经有了更好的解释,但我觉得自己要写出来。]


" yield"是一个关键字。简单来说,如果收益,出现在

函数内,该函数成为一个可以返回多个

结果的生成器。


考虑以下简单的例子:计算某些序列的

元素并打印每个元素的函数。


def square(n):

for i在范围(n):

打印i ** 2


现在假设除了 - 或者代替 - 打印

这个序列的元素,调用者想要做一些事情

与它们不同。您可以通过构建一个列表来处理这个问题,并且

将列表返回给调用者以循环并操作,因为他们选择了b $ b。但是如果列表非常大,那就不是非常节省内存的,如果

,调用者一次只需要对一个元素进行操作。


这就是收益率的来源。如果你这样写:


def square(n):

for i in range(n):

收益率i ** 2

square现在是一个生成器函数。如果你调用square(100),它就不会立即计算任何序列元素。它将返回一个

迭代器,调用者可以循环:


for el in square(100):

print el #或者做其他任何打电话者决定的事情


这样做的好处在于你实现了生成序列元素和使用它们之间的物理隔离。发电机功能

只关心如何生产元素,而来电者只关心

如何使用它们。


希望这会有所帮助,


Carsten。


Hi.

may someone explain "yield" function, please. how does it actually work
and when do you use it?

thanks in forward

mateusz

解决方案

Mateuszk87 wrote:

may someone explain "yield" function, please. how does it actually work
and when do you use it?

it returns a value from a function without actually terminating the
function; when the function is resumed, it''ll continue to execute after
the yield.

a function that contains a yield statement is called a "generator", and
is most often used in a for-in loop, or in other contexts that expect a
sequence. the loop is automatically terminated when the function
returns in a usual way:

>>def gen():

.... yield 1
.... yield 2
.... yield 3
....

>>for item in gen():

.... print item
....
1
2
3

>>sum(gen())

6

>>[str(i) for i in gen()]

[''1'', ''2'', ''3'']

you can also use the generator "by hand"; when you call a generator
function, it returns a special "generator object", and then immediately
suspends itself. to run the generator, call its "next" method:

>>g = gen()
g

<generator object at 0x00AE64E0>

>>g.next()

1

>>g.next()

2

>>g.next()

3

when the generator is exhausted, it raises a StopIterator exception:

>>g.next()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

reference information:

http://effbot.org/pyref/yield.htm

hope this helps!

</F>


Fredrik Lundh wrote:

reference information:

also see:

http://effbot.org/pyfaq/what-is-a-generator.htm

</F>


On Wed, 2006-11-15 at 09:13 -0800, Mateuszk87 wrote:

Hi.

may someone explain "yield" function, please. how does it actually work
and when do you use it?

[There is probably a much better explanation of this somewhere on the
net already, but I feel like writing this out myself.]

"yield" is a keyword. In simple terms, if "yield" appears inside a
function, the function becomes a generator that can return multiple
results.

Consider the following trivial example of a function that calculates the
elements of some sequence and prints each one.

def squares(n):
for i in range(n):
print i**2

Now suppose it turns out that in addition to--or instead of--printing
the elements of this sequence, the caller wants to do something
different with them. You could handle this by building a list, and
return the list to the caller to loop over and operate on as they
choose. But if the list is very big, that''s not very memory-efficient if
the caller only needs to operate on one element at a time.

This is where yield comes in. If you write this:

def squares(n):
for i in range(n):
yield i**2

squares is now a generator function. If you call squares(100), it won''t
calculate any sequence elements immediately. It will instead return an
iterator the caller can loop over:

for el in squares(100):
print el # or do anything else the caller decides

The beauty of this is that you achieve physical separation between
generating elements of a sequence and using them. The generator function
only cares about how to produce the elements, and the caller only cares
about how to use them.

Hope this helps,

Carsten.


这篇关于产量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆