What is the difference between return x,y and return (x,y) in a function?

Viewed 831

I am watching Introduction to Computer Science and Programming in Python of MIT Open Course Ware. In Lecture 5 which introduces Tuples, the Professor says that you are only allowed to return one object in functions. And then she says, tuples are handy for returning more than one object values.(For anyone who want to watch this short part of the lecture which starts at 06:15 min). The question is regarding the MIT Lecture:

Yes, we can use Tuples for returning and collecting a couple of values. On the other hand, a function in Python already can return more than one value by separating them by commas. As a result, I am confused with what the lecturer said. Is returning 2 or more values by separating with commas mean that these objects are becoming an item of a tuple although it is not declared explicitly? What am I missing? In other words, what is the difference between separating with comma as x,y and in parenthesis as (x,y). You can provide explanation by using the function below. Thanks.

def smth(x,y):

   x = y % 2
   y = x % 5

   return x, y
5 Answers

Yes, they are becoming tuple. If you run the following code you will find that the type is tuple

def smth(x,y):

   x = y % 2
   y = x % 5

   return x, y

x=smth(10,20)
print(x)
print(type(x))

Your return statement return x, y is actually just a shortcut for return (x, y).

The python interpreter interprets those equally.

>>> def smth(x,y):
...    x = y%2
...    y = x%2
...    return x,y

>>> smth(1,2)
(0, 0)
>>> type(smth(1,2))
<class 'tuple'>

The automatic unpacking of arguments is done in the same way:

a,b = smth(1,2) and (a, b) = smth(1,2) is equivalent code.

However, the first one is much more readable.

A concrete explanation why those two statements are equal can be found in this answer to a similar question.

The function you wrote is, in fact, returning a tuple. Parentheses are only required for the empty tuple. See this spot in the docs: 6.2.3. Parenthesized forms

By separating them with a comma, a tuple is created. However you can't create an empty tuple this way.. for that you need to use parenthesis.

Note that when you print a tuple, python will output it surrounded by parens whether you created it using parens or not:

>>> mytuple = (1, 2)
>>> print(mytuple)
(1, 2)

>>> mytuple = ()
>>> print(mytuple)
()

>>> mytuple = 1,2
>>> print(mytuple)
(1, 2)

>>> mytuple = 1,
>>> print(mytuple)
(1,)

For this reason, personally I prefer to use parens in most places, it's just more obvious and consistent. But, others may disagree. In any case, it's not always required.

So, in your code, whether you use:

return x, y

or

return (x, y)

.. the result is that a tuple with two elements will be returned.

When calling your function, you can unpack the tuple or keep it as-is:

With unpacking:

x_value, y_value = smth(x, y)

Without unpacking:

xy_tuple = smth(x, y)

x_value = xy_tuple[0]
y_value = xy_tuple[1]

If you define a function like you mentioned, with

return x, y

or

return (x, y)

The python interpreter will recognize them the same way

type(smth(21,11))

will be

<class 'tuple'>
Related