[Tutor] Returning multiple objects from a function
Alan Gauld
alan.gauld at btinternet.com
Tue Jul 3 01:14:06 CEST 2012
On 02/07/12 23:55, Alexander Q. wrote:
> For example, if I have "return(list1, list2, list 3)" within a function
> "mainFunc()" that takes no arguments, how do I use list1, list2, and
> list3 outside of the function
Just assign the result to 3 variables:
L1,L2,L3 = mainFunc()
This is just a special case of tuple unpacking which means you can do:
a,b,c = 1,2,3
to set a=1, b=2, c=3
The function returns a tuple of values, so you unpack them into variables.
Alternatively you can keep them as a tuple:
values = mainFunc()
and use indexing:
print values[0], values[1], values[2]
or iterate over them:
for value in mainFunc():
print value
Lots of options depending on what you want to do...
HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list