3 ms·
The nice thing about Python generator functions was that they didn't need a lot of syntax - if a function contains the "yield" keyword, it's a generator, otherw
by thristian 11y ago
The nice thing about Python generator functions was that they didn't need a lot of syntax - if a function contains the "yield" keyword, it's a generator, otherwise it's not.
Given how closely tied generators are with async/await, why do we need "async def"? Isn't the presence of "await" or "async for" or "async with" enough to mark a function as asynchronous?
- ploxiln 11y agoThere's already a decorator @asyncio.coroutine which "async def" replaces. It's used for error checking, but also for some functions which are coroutines but don't use "yield" - they just return a Future
- drothlis 11y agoA major motivation for the new syntax is to support refactoring (currently moving "yield" into a sub-function makes the calling function no longer a generator). See the PEP's "rationale and goals"[1] and this article[2] summarising the PEP. [1] https://www.python.org/dev/peps/pep-0492/#rationale-and-goals https://www.python.org/dev/peps/pep-0492/#rationale-and-goal... [2] https://lwn.net/Articles/643786/ https://lwn.net/Articles/643786/