How does Python return multiple values from a function?
<p>I have written the following code:</p>

<pre><code>class FigureOut:
   first_name = None
   last_name = None
   def setName(self, name):
      fullname = name.split()
      self.first_name = fullname[0]
      self.last_name = fullname[1]

   def getName(self):
      return self.first_name, self.last_name

f = FigureOut()
f.setName("Allen Solly")
name = f.getName()
print (name)
</code></pre>

<p>I get the following <strong>Output:</strong></p>

<pre><code>('Allen', 'Solly')
</code></pre>

<p>Whenever multiple values are returned from a function in python, does it always convert the multiple values to a <strong>list of multiple values</strong> and then returns it from the function?</p>

<p><em>Is the whole process same as converting the multiple values to a <code>list</code> explicitly and then returning the list, for example in JAVA, as one can return only one object from a function in JAVA?</em></p>

<p>From <a href="https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch07s04.html" rel="nofollow">Phyton Cookbook v.30</a></p>

<pre><code>def myfun():
    return 1, 2, 3
</code></pre>

<p></p>

<pre><code>a, b, c = myfun()
</code></pre>

<blockquote>
  <p><strong>Although it looks like <code>myfun()</code> returns multiple values, a <code>tuple</code> is actually being created.</strong> It looks a bit peculiar, but itâs actually the comma that forms a tuple, not the parentheses</p>
</blockquote>

<p>So yes, what's going on in <a href="/questions/tagged/phyton" class="post-tag" title="show questions tagged &#39;phyton&#39;" rel="tag">phyton</a> is an internal transformation from multiple comma separated values to a tuple and viceversa. </p>

<p>Though there's no equivalent in <a href="/questions/tagged/java" class="post-tag" title="show questions tagged &#39;java&#39;" rel="tag">java</a> you can easily create this behaviour using <code>array</code>'s or some <code>Collection</code>s like <code>List</code>s:</p>

<pre><code>private static int[] sumAndRest(int x, int y) {
    int[] toReturn = new int[2];

    toReturn[0] = x + y;
    toReturn[1] = x - y;

    return toReturn;

}
</code></pre>

<p>Executed in this way:</p>

<pre><code>public static void main(String[] args) {
    int[] results = sumAndRest(10, 5);

    int sum  = results[0];
    int rest = results[1];

    System.out.println("sum = " + sum + "\nrest = " + rest);

}
</code></pre>

<p>result:</p>

<pre><code>sum = 15
rest = 5
</code></pre>

python function python-3.x return