我念叨發電機下面的教程在Python http://excess.org/article/2013/02/itergen2/Python生成器 - float((yield))?
它包含以下代碼:
def running_avg():
"coroutine that accepts numbers and yields their running average"
total = float((yield))
count = 1
while True:
i = yield total/count
count += 1
total += i
我不明白的float((yield))
意義。我認爲yield
被用來從發生器「返回」一個值。這是yield
的不同用法嗎?
http://docs.python.org/2/reference/expressions.html#generator-iterator-methods –