Files
example-code-2e/19-coroutine/coroutil.py
2021-05-21 18:56:12 -03:00

13 lines
322 B
Python

# tag::CORO_DECO[]
from functools import wraps
def coroutine(func):
"""Decorator: primes `func` by advancing to first `yield`"""
@wraps(func)
def primer(*args, **kwargs): # <1>
gen = func(*args, **kwargs) # <2>
next(gen) # <3>
return gen # <4>
return primer
# end::CORO_DECO[]