On Tuesday, June 2, 2015 at 2:27:37 PM UTC-7, fl wrote:
> Hi,
>
> I just see the tutorial says Python can return value in function, it does
> not say multiple data results return situation. In C, it is possible.
> How about Python on a multiple data return requirement?
>
>
> Thanks,
You return a tuple, set, or other iterable. For example:
def return_two_values():
return 1, 2
a, b = return_two_values()
print a
print b
This would print:
1
2
Note though that when doing something like this, you have to be really careful
that if you have multiple calls to `return` in your function, that they will
ALL return the same number of values. Otherwise, when the tuple/list/etc. is
unpacked, you'll get an error.
def return_two_values():
# ... do some stuff
if someCondition:
print "someCondition was true!"
return 0
return 1, 2
a, b = return_two_values()
Here, if someCondition ended up being False, then an exception would be thrown.
Keep in mind that the unpacking of the returned value into the variables `a`
and `b` works with *ANY* iterable. So if you returned 'abc' and unpacked it
into three variables, then the first would contain 'a', the second 'b', and the
third 'c'.
You can also just return a dictionary if you want to return multiple values.
--
https://mail.python.org/mailman/listinfo/python-list