Difference between generators and functions returning generators
<p>I was debugging some code with generators and came to this question. Assume I have a generator function</p>

<pre><code>def f(x):
    yield x
</code></pre>

<p>and a function returning a generator:</p>

<pre><code>def g(x):
    return f(x)
</code></pre>

<p>They surely return the same thing. Can there be any differences when using them interchangeably in Python code? Is there any way to distinguish the two (without <code>inspect</code>)?</p>

<p>They will act the same. And about way to distinguish the two (without <code>inspect</code>). In python? Only inspect:</p>

<pre><code>import inspect

print inspect.isgeneratorfunction(g) --&gt; False
print inspect.isgeneratorfunction(f) --&gt; True
</code></pre>

<p>Of course you can also check it using <code>dis</code>:</p>

<pre><code>Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; def f(x):
...     yield x
... 
&gt;&gt;&gt; def g(x):
...     return f(x)
... 
&gt;&gt;&gt; import dis
&gt;&gt;&gt; dis.dis(f)
  2           0 LOAD_FAST                0 (x)
              3 YIELD_VALUE         
              4 POP_TOP             
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        
&gt;&gt;&gt; dis.dis(g)
  2           0 LOAD_GLOBAL              0 (f)
              3 LOAD_FAST                0 (x)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE  
</code></pre>

<p>but <code>inspect</code> is more appropriate.</p>

python generator