04-yield与send

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》中文版翻译真是让人摸不着头脑啊。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容