-
12 votesanswersviews
`from foo()`和`for f in foo()之间的区别:yield x`
在Python中,大多数收益率的例子都说明了这一点 yield from foo() 类似于 for x in foo(): yield x 另一方面,它并没有引发一些魔法 . 我觉得使用一个我不懂的魔法的函数有点不安 . 我怎么知道 yield from 的魔力,以避免陷入魔法做我不期望的事情?我应该注意到魔法提供了哪些优势? -
0 votesanswersviews
为什么这个使用yield的Python程序不起作用?
我开始使用以下基本代码(如http://aaugustin.github.io/websockets/中的示例代码): import asyncio import websockets @asyncio.coroutine def servePlayer(websocket, path): yield from websocket.send("Hello World"... -
3 votesanswersviews
从具有返回<value>语句的生成器中获取
我有一个带有 return value 语句的生成器 . 如果我使用下一个,我会按预期得到Stopite: value . 但是,当我使用 yield from 时, value 丢失了 . In [1]: def test(): ...: return 1 ...: yield 2 ...: In [2]: t = test() In [3]: t Out... -
1 votesanswersviews
什么's the difference between '产生' and '来自'在Python中?
我在代码中使用了基本的 yield 关键字来构建生成器和协程... 在网上搜索各种 yield 相关解决方案时,我也找到了 yield from 语法 . 两者都用于发电机 . 我不想在这里错过一个功能,那么 from 如何调整基本的 yield ? -
24 votesanswersviews
从流中产生的正确方法是什么?
我有一个 Connection 对象,用于包含 asyncio 连接的读写流: class Connection(object): def __init__(self, stream_in, stream_out): object.__init__(self) self.__in = stream_in self.__out = stre... -
0 votesanswersviews
'yield from'在Python 2中替代
我的代码在 recursive calls 的 python3 中使用了yield from,它完全正常 . 现在的问题是,这是从_797334_ python 3.3中引入的,我需要它在python 2.7中工作 . 我读了几篇文章,其中没有一篇足够详细或简单 . 很少提到文章: Converting “yield from” statement to Python 2.7 code y... -
2 votesanswersviews
不确定为什么StopIteration不由“yield from”处理
查看"Fluent Python"中解释 yield from 的"bidirectional tunnel"功能的以下示例代码,我有以下问题 . from collections import namedtuple Result = namedtuple('Result', 'count average') # the subgenerator de...