python - Python协程在产量上给出未知的无
问题描述
我正在尝试根据用户输入构建一些数学运算的管道,并尝试打印此运算的累积结果。例如,输入将是一个操作列表,然后是数字输入的数量,然后是这样的数字:
[square, accumulate]
3
1
2
3
这应该返回类似:
1
5
14
首先它将 1 打印为 1*1,然后将 1 与 2*2 的结果相加,得到 5,然后将其与 3*3 相加,得到 14。但是我的方法有问题,第二个数字输入总是转动变成 None 值,我不知道为什么。我被困在:
1
None
10
有任何想法吗?这是我的代码:
import math
import os
import random
import re
import sys
def printer():
while True:
x = yield
print(x)
def co_generator(n):
for _ in range(n):
x = int(input())
yield x
def get_root():
while True:
number = (yield)
yield math.floor(math.sqrt(number))
def get_square():
number = 0
while True:
number = (yield)
yield number**2
def accumulator():
acum = 0
while True:
acum += (yield)
yield acum
def operations_pipeline(numbers, operations, print_acum):
for num in numbers:
for i, w in enumerate(operations):
num = w.send(num)
print_acum.send(num)
for operation in operations:
operation.close()
print_acum.close()
if __name__ == '__main__':
order = input().strip()
n = int(input())
numbers = co_generator(n)
print_acum = printer()
next(print_acum)
root = get_root()
next(root)
accumulate = accumulator()
next(accumulate)
square = get_square()
next(square)
operations_pipeline(numbers, eval(order), print_acum)
解决方案
您正在编写代码,就像(yield)
接收一个值并yield whatever
发送一个值一样。这不是它的工作原理。所有yield
s 都发送一个值并接收一个值。
当生成器执行 ayield
时,它分两个阶段执行。首先, 的参数yield
成为当前next
或send
调用的返回值,生成器暂停执行。如果没有参数,None
则使用。那就是你None
的 s 来自哪里
其次,当另一个next
orsend
被执行时,生成器取消挂起,并且send
参数(或None
如果next
被使用)成为yield
表达式的值。
您正在尝试使用一个yield
来接收send
参数,另一个来设置send
的返回值。您需要使用 singleyield
来设置send
返回值并接收 nextsend
的参数。例如,
def get_square():
number = 0
while True:
number = yield number**2
或者,如果您想使用单独yield
的 s 在生成器端发送和接收值,那么您需要使用单独的send
(或next
)调用在另一端接收和发送值,并忽略None
s. 例如,
w.send(num)
num = next(w)
而不是num = w.send(num)
,所以operations_pipeline
看起来像
def operations_pipeline(numbers, operations, print_acum):
for num in numbers:
for w in operations:
w.send(num)
num = next(w)
print_acum.send(num)
for operation in operations:
operation.close()
print_acum.close()
推荐阅读
- python - 从 Stata Do 文件返回值到 Python
- android - 第一次点击时在同一个按钮上更改文本大小,第二次点击时更改为默认值
- c - 空指针和动态内存分配
- wordpress - 在 Apache 上通过 HTTPS 将服务器别名重定向到服务器名称
- c# - ASP.NET C# 在 MongoDB 的单个字段中插入多个值
- python - 在 python 中使用 googletranslate 时出现无效的源语言错误
- c# - 如何使用预制更改文本?
- android - 如何在 Android 中使用 Coroutine 正确等待侦听器
- sql - 使用多个连接查找最小值
- android - 您的应用正在使用 HostnameVerifier 接口的不安全实现如何解决