I have written the following code:
class FigureOut:
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)
I get the following Output:
('Allen', 'Solly')
Whenever multiple values are returned from a function in python, does it always convert the multiple values to a list of multiple values and then returns it from the function?
Is the whole process same as converting the multiple values to a list
explicitly and then returning the list, for example in JAVA, as one can return only one object from a function in JAVA?
This question is related to
python
function
python-3.x
return
Python functions always return a unique value. The comma operator is the constructor of tuples so self.first_name, self.last_name
evaluates to a tuple and that tuple is the actual value the function is returning.
mentioned also here, you can use this:
import collections
Point = collections.namedtuple('Point', ['x', 'y'])
p = Point(1, y=2)
>>> p.x, p.y
1 2
>>> p[0], p[1]
1 2
Whenever multiple values are returned from a function in python, does it always convert the multiple values to a list of multiple values and then returns it from the function??
I'm just adding a name and print the result that returns from the function. the type of result is 'tuple'.
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]
self.special_name = fullname[2]
def getName(self):
return self.first_name, self.last_name, self.special_name
f = FigureOut()
f.setName("Allen Solly Jun")
name = f.getName()
print type(name)
I don't know whether you have heard about 'first class function'. Python is the language that has 'first class function'
I hope my answer could help you. Happy coding.
Here It is actually returning tuple
.
If you execute this code in Python 3:
def get():
a = 3
b = 5
return a,b
number = get()
print(type(number))
print(number)
Output :
<class 'tuple'>
(3, 5)
But if you change the code line return [a,b]
instead of return a,b
and execute :
def get():
a = 3
b = 5
return [a,b]
number = get()
print(type(number))
print(number)
Output :
<class 'list'>
[3, 5]
It is only returning single object which contains multiple values.
There is another alternative to return
statement for returning multiple values, use yield
( to check in details see this What does the "yield" keyword do in Python?)
Sample Example :
def get():
for i in range(5):
yield i
number = get()
print(type(number))
print(number)
for i in number:
print(i)
Output :
<class 'generator'>
<generator object get at 0x7fbe5a1698b8>
0
1
2
3
4
From Python Cookbook v.30
def myfun():
return 1, 2, 3
a, b, c = myfun()
Although it looks like
myfun()
returns multiple values, atuple
is actually being created. It looks a bit peculiar, but it’s actually the comma that forms a tuple, not the parentheses
So yes, what's going on in Python is an internal transformation from multiple comma separated values to a tuple and vice-versa.
Though there's no equivalent in java you can easily create this behaviour using array
's or some Collection
s like List
s:
private static int[] sumAndRest(int x, int y) {
int[] toReturn = new int[2];
toReturn[0] = x + y;
toReturn[1] = x - y;
return toReturn;
}
Executed in this way:
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);
}
result:
sum = 15
rest = 5
Source: Stackoverflow.com