Why does Python3.5 have generator based co-routines?
<p>If at all, there's no functional differences (besides syntax) between <code>native</code> and <code>generator</code> based co-routines; why does <code>Python3</code> have both? I understand what a generator based co-routine is.</p>

<p>Was there a particular design decision or thought that made sense to have both? </p>

<p>Also, I'm a bit confused here; the diff between a generator and a co-routine is that I can't write into a <code>generator</code>; this <a href="https://www.python.org/dev/peps/pep-0342/" rel="nofollow">PEP</a> added the ability to do that. I guess there's no diff between the two in python? (I'm also assuming that writing into a co-routine and sending a value to a co-routine is the same thing)</p>

<p>Why does <code>asyncio</code> support the use of both?</p>

<p>Before 3.5, there was no real difference between a generator and a co-routine. A generator <em>is</em> a co-routine purely by using <code>yield</code> as an expression and expecting data via <code>send()</code>.</p>

<p>This sort-of changed with the new <a href="https://www.python.org/dev/peps/pep-0492/" rel="nofollow"><code>async</code> syntax</a> added to Python 3.5. The <code>async</code> syntax is both syntactic sugar (turning generators into async coroutines visually in your code) and an extension to the language to make it possible to use coroutines as context managers and iterables (<code>async with</code> and <code>async for</code>).</p>

<p>Quoting the PEP:</p>

<blockquote>
  <p>It is proposed to make <em>coroutines</em> a proper standalone concept in Python, and introduce new supporting syntax. The ultimate goal is to help establish a common, easily approachable, mental model of asynchronous programming in Python and make it as close to synchronous programming as possible.</p>
</blockquote>

<p>Under the hood, co-routines are basically still generators and generators support most of the same functionality. Co-routines are now a distinct type however to make it possible to explicitly test for awaitable objects, see <a href="http://bugs.python.org/issue24400" rel="nofollow">issue 24400</a> for the motivation (initially this was a per-instance flag, not a distinct type).</p>

<p>To be clear, you can still use <code>.send()</code> an a generator. But a generator is not <em>awaitable</em>, it is not <em>expected</em> to cooperate.</p>

<p><code>asyncio</code> must support both because the library aims to be compatible with Python versions &lt; 3.5 and thus can't rely on the new language constructs to be available.</p>

python generator python-asyncio coroutine