title: Pythonic way to efficiently handle variable number of return args
body: <p>So I have a function that can either work quietly or verbosely.  In quiet mode it produces an output.  In verbose mode it also saves intermediate calculations to a list, though doing so takes extra computation in itself.  </p>
  
  <p>Before you ask, yes, this is an identified bottleneck for optimization, and the verbose output is rarely needed so that's fine.  </p>
  
  <p>So the question is, what's the most pythonic way to efficiently handle a function which may or may not return a second value?  I suspect a pythonic way would be named tuples or dictionary output, e.g.</p>
  
  <pre><code>def f(x,verbose=False):
      result = 0
      verbosity = []
      for _ in x:
          foo = # something quick to calculate
          result += foo
          if verbose:
              verbosity += # something slow to calculate based on foo
      return {"result":result, "verbosity":verbosity}
  </code></pre>
  
  <p>But that requires constructing a dict when it's not needed.</p>
  
  <p>Some alternatives are:</p>
  
  <pre><code># "verbose" changes syntax of return value, yuck!
  return result if verbose else (result,verbosity)
  </code></pre>
  
  <p>or using a mutable argument</p>
  
  <pre><code>def f(x,verbosity=None):
      if verbosity:
          assert verbosity==[[]]
      result = 0
      for _ in x:
          foo = # something quick to calculate
          result += foo
          if verbosity:
              # hard coded value, yuck
              verbosity[0] += # something slow to calculate based on foo
      return result
  
  # for verbose results call as
  verbosity = [[]]
  f(x,verbosity)
  </code></pre>
  
  <p>Any better ideas?</p>