title: Alternatives for returning multiple values from a Python function
body: <p>The canonical way to return multiple values in languages that support it is often <a href="https://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>.</p>
  <h3>Option: Using a tuple</h3>
  <p>Consider this trivial example:</p>
  <pre><code>def f(x):
    y0 = x + 1
    y1 = x * 3
    y2 = y0 ** y3
    return (y0, y1, y2)
  </code></pre>
  <p>However, this quickly gets problematic as the number of values returned increases. What if you want to return four or five values? Sure, you could keep tupling them, but it gets easy to forget which value is where. It's also rather ugly to unpack them wherever you want to receive them.</p>
  <h3>Option: Using a dictionary</h3>
  <p>The next logical step seems to be to introduce some sort of 'record notation'. In Python, the obvious way to do this is by means of a <code>dict</code>.</p>
  <p>Consider the following:</p>
  <pre><code>def g(x):
    y0 = x + 1
    y1 = x * 3
    y2 = y0 ** y3
    return {'y0': y0, 'y1': y1 ,'y2': y2}
  </code></pre>
  <p>(Just to be clear, y0, y1, and y2 are just meant as abstract identifiers. As pointed out, in practice you'd use meaningful identifiers.)</p>
  <p>Now, we have a mechanism whereby we can project out a particular member of the returned object. For example,</p>
  <pre><code>result['y0']
  </code></pre>
  <h3>Option: Using a class</h3>
  <p>However, there is another option. We could instead return a specialized structure. I've framed this in the context of Python, but I'm sure it applies to other languages as well. Indeed, if you were working in C this might very well be your only option. Here goes:</p>
  <pre><code>class ReturnValue:
    def __init__(self, y0, y1, y2):
       self.y0 = y0
       self.y1 = y1
       self.y2 = y2
  
  def g(x):
    y0 = x + 1
    y1 = x * 3
    y2 = y0 ** y3
    return ReturnValue(y0, y1, y2)
  </code></pre>
  <p>In Python the previous two are perhaps very similar in terms of plumbing - after all <code>{ y0, y1, y2 }</code> just end up being entries in the internal <code>__dict__</code> of the <code>ReturnValue</code>.</p>
  <p>There is one additional feature provided by Python though for tiny objects, the <code>__slots__</code> attribute. The class could be expressed as:</p>
  <pre><code>class ReturnValue(object):
    __slots__ = [&quot;y0&quot;, &quot;y1&quot;, &quot;y2&quot;]
    def __init__(self, y0, y1, y2):
       self.y0 = y0
       self.y1 = y1
       self.y2 = y2
  </code></pre>
  <p>From the <a href="http://www.network-theory.co.uk/docs/pylang/__slots__.html" rel="noreferrer">Python Reference Manual</a>:</p>
  <blockquote>
  <p>The <code>__slots__</code> declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because <code>__dict__</code> is not created for each instance.</p>
  </blockquote>
  <h3>Option: Using a <a href="https://docs.python.org/3/library/dataclasses.html" rel="noreferrer">dataclass</a> (Python 3.7+)</h3>
  <p>Using Python 3.7's new dataclasses, return a class with automatically added special methods, typing and other useful tools:</p>
  <pre><code>@dataclass
  class Returnvalue:
      y0: int
      y1: float
      y3: int
  
  def total_cost(x):
      y0 = x + 1
      y1 = x * 3
      y2 = y0 ** y3
      return ReturnValue(y0, y1, y2)
  </code></pre>
  <h3>Option: Using a list</h3>
  <p>Another suggestion which I'd overlooked comes from Bill the Lizard:</p>
  <pre><code>def h(x):
    result = [x + 1]
    result.append(x * 3)
    result.append(y0 ** y3)
    return result
  </code></pre>
  <p>This is my least favorite method though. I suppose I'm tainted by exposure to Haskell, but the idea of mixed-type lists has always felt uncomfortable to me. In this particular example the list is -not- mixed type, but it conceivably could be.</p>
  <p>A list used in this way really doesn't gain anything with respect to the tuple as far as I can tell. The only real difference between lists and tuples in Python is that lists are <a href="http://docs.python.org/library/stdtypes.html#typesseq-mutable" rel="noreferrer">mutable</a>, whereas tuples are not.</p>
  <p>I personally tend to carry over the conventions from functional programming: use lists for any number of elements of the same type, and tuples for a fixed number of elements of predetermined types.</p>
  <h1>Question</h1>
  <p>After the lengthy preamble, comes the inevitable question. Which method (do you think) is best?</p>