Python@coroutine decorator
python中是否内置了协同程序装饰器?我在中看到过类似的东西,但是python中是否有类似的东西Python@coroutine decorator,python,python-3.x,yield,coroutine,Python,Python 3.x,Yield,Coroutine,python中是否内置了协同程序装饰器?我在中看到过类似的东西,但是python中是否有类似的东西 @coroutine def func(): while True: val = yield 这样我就可以调用它并立即向它发送,而无需先使用next。我认为一个基本方法是: def coroutine(func): @functools.wraps(func) def wrapper_coroutine(*args, **kwargs):
@coroutine
def func():
while True:
val = yield
这样我就可以调用它并立即向它发送,而无需先使用next
。我认为一个基本方法是:
def coroutine(func):
@functools.wraps(func)
def wrapper_coroutine(*args, **kwargs):
f = func(*args, **kwargs)
next(f)
return f
return wrapper_coroutine
但是我想知道python是否有我所缺少的内置功能。您可能已经自己回答了这个问题,但是为了其他遇到这个问题的人的利益,我也在寻找同样的东西,并发现了以下内容:
如评论中所述,简单的答案是:;在标准库中似乎没有这样的东西
关于本网站上的主题的答案指向David Beazely的一个实现,为了方便起见,下面列出了一段代码片段
def coroutine(func):
def start(*args,**kwargs):
cr = func(*args,**kwargs)
cr.next()
return cr
return start
coroutine
不是内置名称。我想你看到了:。答案似乎是否定的,在标准库中没有这样的东西。