Python生成器:返回yield

时间:2017-04-19 18:48:17

标签: python generator yield coroutine

在以下代码块中:

def bottom():  
    # Returning the yield lets the value that goes up the call stack to come right back down.
    return (yield 42)

def middle():  
    return (yield from bottom())

def top():  
    return (yield from middle())

# Get the generator.
gen = top()  
value = next(gen)  
print(value)  # Prints '42'.  
try:  
    value = gen.send(value*2)
except StopIteration as exc:  
    value = exc.value
print(value)  # Prints '84'.
  1. 返回(产量42)实际上做了什么?为什么不简单地返回42 为什么(产量42)在括号中?
  2. 他的意思是: "返回yield会使调用堆栈的值上升到 马上回来" ?
  3. 为什么he使用"来自"在"顶部"和 "中间"功能?

0 个答案:

没有答案