python生成器“发送"功能目的? [英] python generator "send" function purpose?

查看:27
本文介绍了python生成器“发送"功能目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能举个例子说明为什么存在与 Python 生成器函数关联的发送"函数?我完全理解屈服函数.但是,发送功能让我感到困惑.关于此方法的文档很复杂:

generator.send(value)

<块引用>

恢复执行并将值发送"到生成器函数中.value 参数成为当前 yield 表达式的结果.send() 方法返回生成器生成的下一个值,或者如果生成器退出而没有生成另一个值则引发 StopIteration.

这是什么意思?我认为值是函数的输入?send() 方法返回由生成器产生的下一个值"这句话似乎也是 yield 函数的确切目的;yield 返回生成器产生的下一个值...

谁能给我一个使用 send 的生成器的例子,它可以完成一些 yield 不能完成的事情?

解决方案

它用于将值发送到刚刚产生的生成器中.这是一个人为的(无用的)解释示例:

<预><代码>>>>def double_inputs():...而真:... x = 产量...产量 x * 2...>>>gen = double_inputs()>>>next(gen) # 运行到第一个产量>>>gen.send(10) # 进入 'x' 变量20>>>next(gen) # 运行到下一个 yield>>>gen.send(6) # 再次进入 'x'12>>>next(gen) # 运行到下一个 yield>>>gen.send(94.3) # 再次进入 'x'188.5999999999999

你不能只用 yield 做到这一点.

至于它为什么有用,我见过的最好的用例之一是 Twisted 的 @defer.inlineCallbacks.本质上,它允许您编写这样的函数:

@defer.inlineCallbacksdef doStuff():结果 = 收益率需要TwoSeconds()nextResult = 收益率需要TenSeconds(结果 * 10)defer.returnValue(nextResult/10)

发生的情况是 takesTwoSeconds() 返回一个 Deferred,这是一个承诺稍后计算的值.Twisted 可以在另一个线程中运行计算.计算完成后,它将其传递给 deferred,然后将值发送回 doStuff() 函数.因此 doStuff() 最终可能看起来或多或少像一个普通的过程函数,除了它可以进行各种计算和;回调等.此功能之前的替代方法是执行以下操作:

def doStuff():returnDeferred = defer.Deferred()def gotNextResult(nextResult):returnDeferred.callback(nextResult/10)定义得到结果(结果):takeTenSeconds(result * 10).addCallback(gotNextResult)takeTwoSeconds().addCallback(gotResult)退货退货延期

它更加复杂和笨拙.

Can someone give me an example of why the "send" function associated with Python generator function exists? I fully understand the yield function. However, the send function is confusing to me. The documentation on this method is convoluted:

generator.send(value)

Resumes the execution and "sends" a value into the generator function. The value argument becomes the result of the current yield expression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value.

What does that mean? I thought value was the input to the function? The phrase "The send() method returns the next value yielded by the generator" seems to be also the exact purpose of the yield function; yield returns the next value yielded by the generator...

Can someone give me an example of a generator utilizing send that accomplishes something yield cannot?

解决方案

It's used to send values into a generator that just yielded. Here is an artificial (non-useful) explanatory example:

>>> def double_inputs():
...     while True:
...         x = yield
...         yield x * 2
...
>>> gen = double_inputs()
>>> next(gen)       # run up to the first yield
>>> gen.send(10)    # goes into 'x' variable
20
>>> next(gen)       # run up to the next yield
>>> gen.send(6)     # goes into 'x' again
12
>>> next(gen)       # run up to the next yield
>>> gen.send(94.3)  # goes into 'x' again
188.5999999999999

You can't do this just with yield.

As to why it's useful, one of the best use cases I've seen is Twisted's @defer.inlineCallbacks. Essentially it allows you to write a function like this:

@defer.inlineCallbacks
def doStuff():
    result = yield takesTwoSeconds()
    nextResult = yield takesTenSeconds(result * 10)
    defer.returnValue(nextResult / 10)

What happens is that takesTwoSeconds() returns a Deferred, which is a value promising a value will be computed later. Twisted can run the computation in another thread. When the computation is done, it passes it into the deferred, and the value then gets sent back to the doStuff() function. Thus the doStuff() can end up looking more or less like a normal procedural function, except it can be doing all sorts of computations & callbacks etc. The alternative before this functionality would be to do something like:

def doStuff():
    returnDeferred = defer.Deferred()
    def gotNextResult(nextResult):
        returnDeferred.callback(nextResult / 10)
    def gotResult(result):
        takesTenSeconds(result * 10).addCallback(gotNextResult)
    takesTwoSeconds().addCallback(gotResult)
    return returnDeferred

It's a lot more convoluted and unwieldy.

这篇关于python生成器“发送"功能目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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