3

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
Ender Ayhan
  • 308
  • 3
  • 14
  • 2
    In your example you are actually returning a tuple. The identifying feature of a tuple is the comma, not the parenthesis. If you look at the type of the return value using `print(type(smth(5,5)))` you can see that it will say `` – Ahmet Jun 12 '20 at 20:07
  • Does this answer your question? [Why does adding a trailing comma after a variable name make it a tuple?](https://stackoverflow.com/questions/3750632/why-does-adding-a-trailing-comma-after-a-variable-name-make-it-a-tuple) – kaya3 Jun 12 '20 at 20:09
  • No, but @Ahmet answered. Thanks btw. – Ender Ayhan Jun 12 '20 at 20:12

5 Answers5

3

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.

qry
  • 457
  • 3
  • 11
2

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))
ksohan
  • 1,165
  • 2
  • 9
  • 23
1

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

0

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]
little_birdie
  • 5,600
  • 3
  • 23
  • 28
0

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'>
Ajay
  • 177
  • 9