Skip to content

协程基础:send()和yield ​

python
def accumulator():
    total = 0
    while True:
        value = yield total
        total += value

acc = accumulator()
next(acc)  # 启动
print(acc.send(10))  # 10
print(acc.send(20))  # 30

练习:答案

用心记录每一步成长