美文网首页
04-yield与send

04-yield与send

作者: 李文博 | 来源:发表于2017-03-24 22:51 被阅读0次

Technically,yield is now an expression form that returns the item passed to send, not

a statement (though it can be called either way—asyield X, orA = (yield X)).

《learning python》中文版翻译不靠谱,这里自己意译一下。

这句话表达的意思有两点:

1. yield是一个表达式而非语句,是要有返回值的;

2. yield的返回值是传递给send的参数。

如下示例:

当send没有调用时,yield是没有返回值的,故打印了None。当send被调用时,yield的返回值便是send的传入参数(并且进行了下一次的next)。

也就是这一段话所说:

The send method advances to the next item in the series of results, just like__next__, but also provides a way for the caller to communicate with the generator, to affect its operation.

send传入generator信息,进而影响迭代过程。

《learning python》中文版翻译真是让人摸不着头脑啊。

相关文章

网友评论

      本文标题:04-yield与send

      本文链接:https://www.haomeiwen.com/subject/cpjqottx.html