By Rhett via Discussions on Python.org at 05Sep2022 03:17:
Hello and good day to all…yes I am begineer, am still hoping someone
can give a sample as I requested?
Try this:
def sum3(a, b, c):
return a + b + c
main():
total = sum3(1, 2, 3)
print("total =", total)
Here’s a function returning a single value, from 3 inputs. It isn’t
fundamentally very different from your eval()
example, which also has
many input values but returns a pair of output values. Here’s a function
returning a pair:
def min_max(a, b, c):
return min(a, b, c), max(a, b, c)
main():
small, large = min_max(1, 2, 3)
print("smallest =", small, "largest =", large)
I think you’re conflating the returned value with the source value, as
others have mentioned. Let’s try a different approach:
Python functions always return exactly one value.
From that it follows that since you can pass many values as arguments to
a function call, you’re always getting just one value back and so there
can be no inherent correspondence between the number of input values and
the single return value.
Now, that might sound like a fiction, so let me elaborate.
The “returning one value” is pretty obvious with the sum3()
example
above, but less obvious with min_max()
. The min_max()
function
returns one value, it is just that that value is a 2-tuple. I can do
this quite validly:
mm = min_max(1, 2, 3)
Watch:
>>> def min_max(a, b, c):
... return min(a, b, c), max(a, b, c)
...
>>> mm = min_max(1, 2, 3)
>>> type(mm)
<class 'tuple'>
>>> print(len(mm))
2
>>> print(mm)
(1, 3)
>>> small, large = mm
>>> type(small)
<class 'int'>
>>> print(small)
1
What you’re seeing with small, large = mm
is what we call an
unpacking assignment in Python. If there are multiple variable names
on the left of the assignment, the single value on the right of the
assignment is iterated, and the values from the iteration assigned to
each of the variables on the left. And they have to match up!
Observe:
>>> a, = [1]
>>> a
1
Python iterated over a 1-element list, and stuck its values into a
etc. But there’s just one element and just one variable. That comma is
important, syntacticly.
More common:
>>> a, b = 1, 2
>>> print(a)
1
>>> print(b)
2
The right hand is a still a single value, a 2-tuple. More overtly:
>>> a, b = (1, 2)
>>> print(a)
1
>>> print(b)
2
Still a single 2-tuple, but the brackets make it more obvious. But the
brackets are not part of the tuple
syntax, they’re just grouping, the
same as here:
x = 3 * (5 + 7)
The brackets do grouping - they are not part of the addition +
operation.
You can put any iterable value on the right. Here’s a a list, like the
a,
example:
>>> a, b = [1, 2]
>>> print(a)
1
>>> print(b)
2
Still a single value, a single list with 2 elements inside it.
Returning to functions:
>>> mm = min_max(1, 2, 3)
>>> type(mm)
<class 'tuple'>
>>> print(mm)
(1, 3)
>>> small, large = mm
The example which confused you initially was the same, just folded up
into one line:
>>> small, large = min_max(1, 2, 3)
Python is just unpacking the 2-tuple from min_max()
into the 2
variables on the left of the assignment.
Does this clarify what’s happening?
Cheers,
Cameron Simpson cs@cskk.id.au