title: Using generator send() within a for loop
body: <p>I implemented graph traversal as a generator function which yields the node being visited.</p>
  
  <p>Sometimes the user needs to tell the traversal function that the edges outgoing from a particular node shouldn't be followed; in order to support that, the traversal checks the value sent back to it (using generator <code>send()</code> method), and if it's <code>True</code>, regards the node as a leaf for traversal purposes.</p>
  
  <p>The problem is that the simplest user loop is kinda long:</p>
  
  <pre><code># simplified thanks to @tobias_k
  # bfs is the traversal generator function
  traversal = bfs(g, start_node)
  try:
    n = next(traversal)
    while True:
      # process(n) returns True if don't want to follow edges out of n
      n = traversal.send(process(n))
  except StopIteration:
      pass
  </code></pre>
  
  <p>Is there any way to improve this? </p>
  
  <p>I thought something like this should work:</p>
  
  <pre><code>for n in bfs(g, start_node):
    ???.send(process(n))
  </code></pre>
  
  <p>but I feel I'm missing the knowledge of some python syntax.</p>