[英]How to yield results from a nested generator function?
I have a function which yields results as it downloads them.我有一个函数可以在下载结果时产生结果。 For the purposes of this question, lets say I yield a sting once every second but I want a convenience function to wrap my generator:
出于这个问题的目的,假设我每秒产生一次刺痛,但我想要一个方便的函数来包装我的生成器:
import time
def GeneratorFunction(max_val):
for i in range(0,5):
time.sleep(1)
yield "String %d"%i
def SmallGenerator():
yield GeneratorFunction(3)
for s in SmallGenerator():
print s
...why doesn't that just print the 5 strings I'm expecting? ...为什么不只打印我期望的 5 个字符串? Instead it appears to return the generator functio:
相反,它似乎返回生成器函数:
<generator object GeneratorFunction at 0x020649B8>
How can I get this to yield the strings as a normal generator function would?我怎样才能让它像普通生成器函数那样产生字符串?
You may have to use the new yield from
, available since Python 3.3, known as “ delegated generator ”.您可能必须使用新的
yield from
,自 Python 3.3 起可用,称为“ 委托生成器”。
If I understood the question correctly, I came to the same issue, and found an answer elsewhere.如果我正确理解了这个问题,我就会遇到同样的问题,并在其他地方找到答案。
I wanted to do something like this:我想做这样的事情:
def f():
def g():
do_something()
yield x
…
yield y
do_some_other_thing()
yield a
…
g() # Was not working.
yield g() # Was not what was expected neither; yielded None.
…
yield b
I now use this instead:我现在改用这个:
yield from g() # Now it works, it yields x and Y.
I got the answer from this page: Python 3: Using "yield from" in Generators - Part 1 (simeonvisser.com) .我从这个页面得到了答案: Python 3: Using "yield from" in Generators - Part 1 (simeonvisser.com) 。
Can't believe I missed this;不敢相信我错过了这个; The answer is to simply return the generator function with suitable arguments applied:
答案是简单地返回应用了合适参数的生成器函数:
import time
def GeneratorFunction(max_val):
for i in range(0,max_val):
time.sleep(1)
yield "String %d"%i
def SmallGenerator():
return GeneratorFunction(3) # <-- note the use of return instead of yield
for s in SmallGenerator():
print s
Came here looking for a different form of "nested yield" and finally found the hidden answer.来到这里寻找不同形式的“嵌套收益率”,终于找到了隐藏的答案。 Might not be the best but it works.
可能不是最好的,但它确实有效。
I was wanting to yield through a registry tree and here is the solution.我想通过注册表树让步,这是解决方案。
def genKeys(key):
for value in key.values():
yield value
for subkey in key.subkeys():
print(subkey)
for x in genKeys(subkey): #this is the trick
continue
Here's another small example for generating the multiplication table from 1 to 10:这是生成从 1 到 10 的乘法表的另一个小示例:
class Gen1:
def __init__(self, gen2):
self.gen2 = gen2
def __iter__(self):
for a in range(1, 11):
for b in self.gen2:
yield a * b
class Gen2:
def __iter__(self):
for a in range(1, 11):
yield a
def main():
gen2 = Gen2()
gen1 = Gen1(gen2)
for v in gen1:
print(v)
if __name__ == '__main__':
main()
I came looking for another use of nested yields..我来寻找嵌套收益的另一种用途..
list_of_lists = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
def iter_all(foo):
yield foo
if isinstance(foo, list):
for i in foo:
for x in iter_all(i):
yield x
print([i for i in iter_all(list_of_lists)])
output:输出:
[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [1, 2, 3], 1, 2, 3, [4, 5, 6], 4, 5, 6, [7, 8, 9], 7, 8, 9]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.