title: Python-3.2 coroutine: AttributeError: 'generator' object has no attribute 'next'
body: <p>Quoting from <em>Python Essential Reference</em>, by David Beazley, page 20:</p>
  <blockquote>
  <p>Normally, functions operate on a single set of input arguments. However, a function can also be written to operate as a task that processes a sequence of inputs sent to it. This type of function is known as a <em>coroutine</em> and is created by using the <code>yield</code> statement as an expression (yield) as shown in this example:</p>
  </blockquote>
  <pre><code>def print_matches(matchtext):
      print &quot;Looking for&quot;, matchtext
      while True:
          line = (yield)       # Get a line of text
          if matchtext in line:
              print line
  </code></pre>
  <blockquote>
  <p>To use this function, you first call it, advance it to the first (yield), and then start sending data to it using <code>send()</code>. For example:</p>
  </blockquote>
  <pre><code>&gt;&gt;&gt; matcher = print_matches(&quot;python&quot;)
  &gt;&gt;&gt; matcher.next() # Advance to the first (yield)
  Looking for python
  &gt;&gt;&gt; matcher.send(&quot;Hello World&quot;)
  &gt;&gt;&gt; matcher.send(&quot;python is cool&quot;)
  python is cool
  &gt;&gt;&gt; matcher.send(&quot;yow!&quot;)
  &gt;&gt;&gt; matcher.close() # Done with the matcher function call
  </code></pre>
  <p>Based on that information, I wrote this code:</p>
  <pre><code>#!/usr/bin/python3.2
  import sys
  
  def match_text(pattern):
      line = (yield)
      if pattern in line:
          print(line)
  
  x = match_text('apple')
  x.next()
  
  for line in input('&gt;&gt;&gt;&gt; '):
      if x.send(line):
          print(line)
  
  x.close()
  </code></pre>
  <p>But I get an error message like:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;xxx&quot;, line 9, in &lt;module&gt;
      matcher.next() # Advance to the first (yield)
  AttributeError: 'generator' object has no attribute 'next'
  </code></pre>
  <p>Why doesn't this code (or the code from the book, for that matter) work in Python 3.2? It seems that what is supposed to be a coroutine, is treated as a generator instead - why? What is going on here?</p>