title: How can I find out the number of outputs returned by a python function?
body: <p>Imagine there are two Python functions:</p>
  
  <pre><code>def f1(x):
      return x
  
  def f2(x):
      return x,x**2
  </code></pre>
  
  <p>I would like to find out how many outputs are returned by <code>f1</code> and how many outputs are returned by <code>f2</code>.</p>
  
  <p>I would like to avoid the following solution:</p>
  
  <pre><code>result = f1(1)
  no_outputs = len(result) if type(result) == tuple else 1
  </code></pre>
  
  <p>Since this solution fails when a function returns a tuple.</p>
  
  <p>I started playing around with the python <code>ast</code> (abstract syntax tree) library, and was wondering if there was any way to parse the syntax tree for a function to figure out the number of outputs returned?</p>
  
  <p>Something along the lines of:</p>
  
  <pre><code>import inspect
  import ast
  
  src = inspect.getsourcelines(f1)[0]
  
  string = ''.join(src)
  ast_   = ast.parse(string)
  </code></pre>
  
  <p>I explored <code>ast_</code> but am not able to find what the returns are.</p>