I have written the following code:
first_name = None
last_name = None
def setName(self, name):
fullname = name.split()
self.first_name = fullname
self.last_name = fullname
return self.first_name, self.last_name
f = FigureOut()
name = f.getName()
Python will return a
tuple in this case since the return specifies comma separated values. Multiple values can only be returned inside containers.
You can look at the byte code generated from a function returning values like yours by using
dis.dis. For comma separated values, it looks like:
def foo(a, b): return a,b import dis dis.dis(foo) 2 0 LOAD_FAST 0 (a) 3 LOAD_FAST 1 (b) 6 BUILD_TUPLE 2 9 RETURN_VALUE
As you can see the values are first loaded on the stack and then a
BUILD_TUPLE (grabbing the previous
2 elements placed on the stack) is generated. Python knows to create tuple due to thhe commas being present.
You could alternatively specify another return type, for example a list, for this case a
BUILD_LIST is going to be issued following the same semantics as it's tuple equivalent:
def foo(a, b): return [a, b] import dis dis.dis(foo) 2 0 LOAD_FAST 0 (a) 3 LOAD_FAST 1 (b) 6 BUILD_LIST 2 9 RETURN_VALUE
To summarize, one actual object is returned, if that object is of a container type, it in essence can contain multiple values giving the impression of multiple results.