[英]python decorators(TypeError: 'NoneType' object is not an iterator)
下面是我的装饰器和协同程序的 python 程序,
#coroutine decorator
def coroutine_decorator(coroutine_func):
def wrapper(*args,**kwargs):
c=coroutine_func(*args,**kwargs)
next(c)
return c
return wrapper
# Define the coroutine function 'linear_equation' below
@coroutine_decorator
def linear_equation(a, b):
while True:
x=yield
e=a*(x**2)+b
print("Expression, {0}*x^2 + {1}, with x being {2} equals {3}".format(int(a),x,int(b),int(e)))
# Define the coroutine function 'numberParser' below
@coroutine_decorator
def numberParser():
equation1 = linear_equation(3, 4)
equation2 = linear_equation(2, -1)
# code to send the input number to both the linear equations
equation1.send(6.0)
equation2.send(6.0)
def main(x):
n = numberParser()
n.send(x)
if __name__ == "__main__":
x = float(input())
我收到运行时错误,我可以理解出了什么问题,但不确定为什么我收到 TypeError: 'NoneType' object is not an iterator
Traceback (most recent call last):
File "solution.py", line 42, in <module>
res = main(x);
File "solution.py", line 34, in main
n = numberParser()
File "solution.py", line 10, in wrapper
next(c)
TypeError: 'NoneType' object is not an iterator
由于您在装饰函数上调用next
,所以大概您只能装饰一个函数,如果它是一个生成器或者它返回一个迭代器。 numberParser
不包含yield
语句,也不返回任何内容,因此不符合这些要求。
您应该修改numberParser
以便它产生一些东西,或者它返回一个迭代器。
numberParser
协程不会为例程屈服
调整
# code to send the input number to both the linear equations
equation1.send(6.0)
equation2.send(6.0)
到
# code to send the input number to both the linear equations
while True :
x = yield
equation1.send(x)
equation2.send(x)
下面的代码为我工作
# Define the function 'coroutine_decorator' below
def coroutine_decorator(coroutine_func):
def wrapper(*args, **kwdargs):
c = coroutine_func(*args, **kwdargs)
next(c)
return c
return wrapper
# Define coroutine 'linear_equation' as specified in previous exercise
@coroutine_decorator
def linear_equation(a, b):
while True:
x = yield
e = a*(x**2)+b
print('Expression, '+str(a)+'*x^2 + '+str(b)+', with x being '+str(x)+' equals '+str(e))
# Define the coroutine function 'numberParser' below
@coroutine_decorator
def numberParser():
equation1 = linear_equation(3, 4)
equation2 = linear_equation(2, -1)
# code to send the input number to both the linear equations
while True:
x = yield
equation1.send(x)
equation2.send(x)
您为包装器 Def 错误地编写了以下代码。 定义 C 变量和下面的步骤是 sub def 包装器的一部分。
def coroutine_decorator(coroutine_func):
def wrapper(*args, **kwdargs):
c = coroutine_func(*args, **kwdargs)
next(c)
return c
return wrapper
然后我在 numberParse Def 中添加了以下块
while True:
x = yield
equation1.send(x)
equation2.send(x)
你非常接近答案。 记住协程的最基本的:
协程在到达 yield 语句时停止执行
因此,您需要做的就是像这样在 equation1 和 equation2 上调用 yield:
equation1.send(x)
equation2.send(x)
equation1 = yield
equation2 = yield
试试这个。 这在主要功能之前对我有用:
# Define 'coroutine_decorator' below
def coroutine_decorator(coroutine_func):
def wrapper(*args,**kwargs):
c=coroutine_func(*args,**kwargs)
next(c)
return c
return wrapper
# Define coroutine 'linear_equation' as specified in previous exercise
@coroutine_decorator
def linear_equation(a, b):
while True:
x=yield
e=a*(x**2)+b
print("Expression, {0}*x^2 + {2}, with x being {1} equals {3}".format(int(a),x,int(b),float(e)))
# Define the coroutine function 'numberParser' below
@coroutine_decorator
def numberParser():
equation1 = linear_equation(3, 4)
equation2 = linear_equation(2, -1)
# code to send the input number to both the linear equations
while True :
x = yield
equation1.send(x)
equation2.send(x)
def main(x):
n = numberParser()
n.send(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.