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>Here It is actually returning <code>tuple</code>.</p>

<p>If you execute this code in Python 3: </p>

<pre><code>def get():
    a = 3
    b = 5
    return a,b
number = get()
print(type(number))
print(number)
</code></pre>

<p><strong>Output :</strong> </p>

<pre><code>&lt;class 'tuple'&gt;
(3, 5)
</code></pre>

<p>But if you change the code line <code>return [a,b]</code> instead of <code>return a,b</code> and execute : </p>

<pre><code>def get():
    a = 3
    b = 5
    return [a,b]
number = get()
print(type(number))
print(number)
</code></pre>

<p><strong>Output :</strong></p>

<pre><code>&lt;class 'list'&gt;
[3, 5]
</code></pre>

<p>It is only returning single object which contains multiple values. </p>

<p>There is another alternative to <code>return</code> statement for returning multiple values, use <code>yield</code>( to check in details see this <a href="http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python">What does the &quot;yield&quot; keyword do in Python?</a>)</p>

<p><strong>Sample Example :</strong></p>

<pre><code>def get():
    for i in range(5):
        yield i
number = get()
print(type(number))
print(number)
for i in number:
    print(i)
</code></pre>

<p><strong>Output :</strong></p>

<pre><code>&lt;class 'generator'&gt;
&lt;generator object get at 0x7fbe5a1698b8&gt;
0
1
2
3
4
</code></pre>

python function python-3.x return