title: In python is there a way to check if a function is a "generator function" before calling it?
body: <p>Lets say I have two functions:</p>
  
  <pre><code>def foo():
    return 'foo'
  
  def bar():
    yield 'bar'
  </code></pre>
  
  <p>The first one is a normal function, and the second is a generator function. Now I want to write something like this:</p>
  
  <pre><code>def run(func):
    if is_generator_function(func):
       gen = func()
       gen.next()
       #... run the generator ...
    else:
       func()
  </code></pre>
  
  <p>What will a straightforward implementation of <code>is_generator_function()</code> look like? Using the <code>types</code> package I can test if  <code>gen</code> is a generator, but I wish to do so before invoking <code>func()</code>. </p>
  
  <p>Now consider the following case:</p>
  
  <pre><code>def goo():
    if False:
       yield
    else:
       return
  </code></pre>
  
  <p>An invocation of <code>goo()</code> will return a generator. I presume that the python parser knows that the <code>goo()</code> function has a yield statement, and I wonder if it possible to get that information easily.</p>
  
  <p>Thanks!</p>