我有以下类(class):
from tornado import gen
class VertexSync(Vertex):
@wait_till_complete
@gen.coroutine
@classmethod
def find_by_value(cls, *args, **kwargs):
stream = yield super().find_by_value(*args, **kwargs)
aggr = []
while True:
resp = yield stream.read()
if resp is None:
break
aggr = aggr + resp
return aggr
TypeError: types.coroutine() expects a callable
你能告诉我问题是什么吗?
=> 编辑 调用此函数的代码
print(DemoVertex.find_by_value('longitude', 55.0))
最佳答案
问题是 classmethod
做了……有趣的事情。一旦类定义完成,您就在类上有了一个很好的可调用方法,但是在定义期间您有一个类方法对象
,它不是可调用:
>>> a = classmethod(lambda self: None)
>>> a
<classmethod object at 0x10b46b390>
>>> callable(a)
False
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'classmethod' object is not callable
最简单的解决方法是重新排序装饰器,而不是试图将类方法变成协程:
@gen.coroutine
@classmethod
def thing(...):
...
你正试图将一个协程变成一个类方法:
@classmethod
@gen.coroutine
def thing(...):
...
请注意,装饰器是“由内而外”应用的,参见例如Decorator execution order
关于python - 错误 : types. coroutine() expects a callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37995862/